I want to check if all words in a string selection
are present in another string. There will be an arbitrary number of words. This is not an OR. All words MUST be present in the matcher. Order does not matter. For example, when selection
is "John Zeni"
, it must match " John Paul Zeni"
because both "John"
and "Zeni"
are in the matcher. If selection
was just "John"
, then it should match, but since there are multiple words, all words must match. Regex solution is required.
This is what I tried:
selection = "John Zeni"
pattern = selection.split(" ").join("|")
# => "John|Zeni"
/#{Regexp.quote(pattern)}/
# => /John|Zeni/
" John Paul Zeni".match(/#{Regexp.quote(pattern)}/)
# => nil
Why doesn't it match? The problem is with Regexp.quote
I think. It is important that both words match in the matcher. This also should not match:
" John Paul Zeni" =~ /(John|Zach)/
# => 1
See Question&Answers more detail:os