regex - Parsing out a grouping of parentheses with ruby regular expressions -


i trying array of tokens such "((token 1))", "((token 2))". have following code:

sentence = "i had ((an adjective)) sandwich breakfast today. oozed on ((a body part)) , ((a noun))."  token_arr = sentence.scan(/\(\(.*\)\)/) # => ["((an adjective))", "((a body part)) , ((a noun))"] 

the above code not stop match when runs first occurrence of "))" in sentence "it oozed...". think need negative lookahead operator, i'm not sure if right approach.

typical problem. use non-greedy quantifier.

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

alternatively, replace /./ "things other ")"":

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

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -