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
Post a Comment