0

After three hours I'm going to stop banging my head on the wall and ask for help. This ought to be an easy regex, but I just can't quite get my mind around it today.

Given a short string and a few different patterns, I need to extract all the matching patterns from the string, where each pattern is a "whole word".

The patterns are: "YES", "NO", "MAYBE", any sequence of asterisks ['*', '**', '***', '...'], any number from 0 to 999, with the case of the text being case-insensitive.

So if s = 'foo yes and no doo 5 boo **** bar far 13 not but no' the result would be ["yes", "no", "5", "****", "13", "no"]

1 Answer 1

3
s = 'foo yes and no doo 5 boo * *** bar far 13 not but no YESTERDAY'
ms = s.scan(/(?:\b(?:yes|no|\d{1,3})\b|\*+)/i)
ms # => ["yes", "no", "5", "*", "***", "13", "no"]
Sign up to request clarification or add additional context in comments.

5 Comments

This would hit on the not too.
yes, it produces that extra "no" from 'not'... ["yes", "no", "5", "", "**", "13", "no", "no", "YES"].I wasnt sure how to tell it to hit only whole words?
@jpwynn: I overlooked that point in your question, the update should fix it.
it misses the last 'yes' if it's the end of the string... produces ["yes", "no", "5", "", "**", "13", "no"] from s = 'foo yes and no doo 5 boo * *** bar far 13 not but no YES'
it's so close... do you do this all day? cuz i find geenrating a regex makes my head hurt!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.