Home: Perl Programming Help: Regular Expressions:
/\((\([^)]*\))\)/



ball
New User

Jul 19, 2008, 10:00 PM


Views: 8686
/\((\([^)]*\))\)/

Could someone help me parse this. I have the basic idea, but am really confused on the details (particularly the group paren and the stuff inside the single character brackets):

/\((\([^)]*\))\)/

/ - start of the regex

\( - escaped open paren

( - grouping paren (not sure why we need it though)

\( - second escaped paren

[ - single character? Not sure why?

^ - beginning of the line? now i am really confused. How do you have a beginning of line after two parens?

) - end grouping paren

] - end single char

* - have 0 or more of the single char (not sure what the single char was!

.... so forth....

I got this from a quiz to parse a madlib file that had questions in it of the following form:

I wanted to eat ((a number)) hamburgers.

Any explanation would be appreciated!


KevinR
Veteran


Jul 20, 2008, 10:42 AM


Views: 8663
Re: [ball] /\((\([^)]*\))\)/

the pattern in the center is a negated character class:

[^)]*

the ^ is not the beginning of line anchor in this context, it means to not match whatever is inside the square brackets, in this case there is a sinlge right parenthesis ')'.

So essentially it means to match zero or more of anything except a ')'. Its appears to be capturingwhatever is inside of double parenthesis:


Code
$_ =  '((this will match))'; 
/\((\([^)]*\))\)/
print $1;


$1 is what is matched by the grouping parenthesis in the regexp.
-------------------------------------------------