Reputation: 740
I want a regular expression that will extract the words happy and good, both non greedy and both case insensitive.
@a = [" I am very HAppy!!", "sad today..", "happy. to hear about this..", "the day is good", "sad one", "sad story"]
It looks like this works with one word:
@z = @a.join.scan(/\bhappy\b/i)
But when I add in good it does not work as I expect.
@z = @a.join.scan(/\bhappy|good\b/i)
Expect ( happy 2x and good 1x):
@z.size => 3
The result it gives me:
@z.size => 2
Upvotes: 2
Views: 333
Reputation: 434665
You should add parentheses around your alternation so that the \b
s will apply to either happy
or good
as a unit:
\b(happy|good)\b
Then, you probably want to scan each element of the @a
array rather than @a.join
so a map
and flatten
are called for:
@a.map { |s| s.scan(/\b(happy|good)\b/i) }.flatten
# ["HAppy", "happy", "good"]
You could also use a non-capturing group:
\b(?:happy|good)\b
but it won't make any difference in this case.
Upvotes: 7
Reputation: 73
I assume you mean it matches both happy, but not good. This is because your looking at word boundaries, and when you join the string it becomes goodsad.
Remove the word boundary conditions and it should match as expected.
Upvotes: 0