Tuesday, 5 July 2016

ruby - Regex - Match all words in parenthesis



I have the following regular expression that is supposed to match all words that have parenthesis around them (including the parenthesis) but it only matches one case. What am I doing wrong?




"(e), (f), and (g)".match(/\(\w+\)/)
=> #


The output should be:



=> #

Answer




Use scan() instead. It returns an array with all the matches. match() will only return the first match.



"(e), (f), and (g)".scan(/\(\w+\)/)

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...