简体   繁体   中英

regex format for two strings

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM