Reputation:
I am trying to get an array of tokens such as "((token 1))"
, "((token 2))"
. I have the following code:
sentence = "I had a ((an adjective)) sandwich for breakfast today. It oozed all over my ((a body part)) and ((a noun))."
token_arr = sentence.scan(/\(\(.*\)\)/)
# => ["((an adjective))", "((a body part)) and ((a noun))"]
The above code does not stop the match when it runs into the first occurrence of "))"
in the sentence "It oozed..."
. I think I need a negative lookahead operator, but I'm not sure if this is the right approach.
Upvotes: 0
Views: 239
Reputation: 15000
try this regex which will only pull non round brackets from the matched inner text
[(]{2}([^()]*)[)]{2}
Upvotes: 1
Reputation: 168101
Typical problem. Use non-greedy quantifier.
sentence.scan(/\(\(.*?\)\)/)
Alternatively, replace /./
with "things other than ")"
":
sentence.scan(/\(\([^)]*\)\)/)
Upvotes: 1