Skip to content

Conversation

@jonahroth
Copy link

@kmodlich @maxdignan regexes are complicated

@@ -1,3 +1,7 @@
def word_in_string?(word, string)
# implement with your code here
if (/(\A|\s|_|-)#{word}(\z|\s|_|-)/ =~ string).nil?
Copy link

@maxdignan maxdignan May 27, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe look into a simpler way to do this regex. Remember, you only need to check for whitespace before and after the word (or nothing on either (or both) sides). http://regexone.com/lesson/whitespaces

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you are not already using something like this http://rubular.com/ go ahead and give it a try. Unless the regex is simple you probably won't be getting it right on the first time you write it, there are many websites that allow you to test your regex on the fly its also great for learning.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice site! I threw together a quick solution. @jonahrr Try out the \b selector

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! My impression from the question was that dashes, underscores, and the beginning or end of a string are also valid delimiters, which is why the regex is complicated as it is.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't look up \b until after I posted that, that's perfect thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants