Reputation: 3154
I have the following string:
"lotfan talash nakonid Left in jomalat ra bekhanid Right zira bi mani hastand Right ..."
I need to get all of the occurrences of "a set of characters followed by Left or Right". So for the above string I need to get the following array:
["lotfan talash nakonid Left", "in jomalat ra bekhanid Right", "zira bi mani hastand Right", ... ]
I tried to write it using scan method:
str.scan(/.*?(Right|Left)/) # => [["Left"], ["Right"], ["Right"]]
But unfortunately it seems that the parenthesis have some special meaning to scan
method.
Is there any way we write this regexp the same way we write it in the match
method?
Upvotes: 1
Views: 297
Reputation: 5742
This should accomplish what you are trying to do:
string.scan(/(.*?(Right|Left))/).map{|arr| arr.join(" ")}
The extra parentheses also capture the text before the "right" or "left".
However, the non-capturing answers are more concise and clean.
Upvotes: 1
Reputation: 12558
In your regex, you have a capture group here: (Right|Left)
. The String#scan
method will use the captures instead of the entire match if captures are present. You should turn (Right|Left)
into a non-capturing group: (?:Right|Left)
str = "lotfan talash nakonid Left in jomalat ra bekhanid Right zira bi mani hastand Right ..."
str.scan(/.*?(?:Right|Left)/)
# => => ["lotfan talash nakonid Left", " in jomalat ra bekhanid Right", " zira bi mani hastand Right"]
Upvotes: 1
Reputation: 1748
You could also use non-capturing groups, like so:
myString.scan(/.*?(?:Right|Left)/)
The documentation (which I linked in a comment above) explains what is happening:
If the pattern contains no groups, each individual result consists of the matched string, $&. If the pattern contains groups, each individual result is itself an array containing one entry per group.
Upvotes: 2