user1096557
user1096557

Reputation:

Parsing out a grouping of parentheses with ruby regular expressions

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

Answers (2)

Ro Yo Mi
Ro Yo Mi

Reputation: 15000

try this regex which will only pull non round brackets from the matched inner text

[(]{2}([^()]*)[)]{2}

enter image description here

enter image description here

Upvotes: 1

sawa
sawa

Reputation: 168101

Typical problem. Use non-greedy quantifier.

sentence.scan(/\(\(.*?\)\)/)

Alternatively, replace /./ with "things other than ")"":

sentence.scan(/\(\([^)]*\)\)/)

Upvotes: 1

Related Questions