Gp De Ciantis
Gp De Ciantis

Reputation: 103

Matching braces in ruby with a character in front

I have read quite a few posts here for matching nested braces in Ruby using Regexp. However I cannot adapt it to my situation and I am stuck. The Ruby 1.9 book uses the following to match a set of nested braces

/\A(?<brace_expression>{([^{}]|\g<brace_expression>)*})\Z/x

I am trying to alter this in three ways. 1. I want to use parentheses instead of braces, 2. I want a character in front (such as a hash symbol), and 3. I want to match anywhere in the string, not just beginning and end. Here is what I have so far.

/(#(?<brace_expression>\(([^\(\)]|\g<brace_expression>)*\)))/x

Any help in getting the right expression would be appreciated.

Upvotes: 2

Views: 451

Answers (1)

sepp2k
sepp2k

Reputation: 370445

Using the regex modifier x enables comments in the regex. So the # in your regex is interpreted as a comment character and the rest of the regex is ignored. You'll need to either escape the # or remove the x modifier.

Btw: There's no need to escape the parentheses inside [].

Upvotes: 2

Related Questions