Regexes

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Can anybody give me a really simple explanation or example of how the
behaviour of the regular expression grouping construct (?<group1-group2>***)
works?

I've read in the documentation that it 'replaces the previously defined
definition of one group with another' ... but I couldn't understand how the
whole of the description related to how it works in the real world.
Apparently it's used for knowing how many nesting levels deep of brackets a
match occurs in, which I can imagine would be really useful in some
scenarios, but I'm lost as to how to use it.

Anybody know?
 
Patty O'Dors said:
Can anybody give me a really simple explanation or example of how the
behaviour of the regular expression grouping construct
(?<group1-group2>***)
works?

I've more or less copied this from J. Friedl's "Mastering Regular
Expressions". (Great book!)

Consider this regex:
\(
(?>
[^()]+
|
\( (?<UNMATCHED>)
|
\) (?<MATCHED-UNMATCHED>)
)*
(?(UNMATCHED)(?!))
\)

Everytime '(' is found, a capture is added to the "UNMATCHED" group.
Everytime ')' is found, the last capture from "UNMATCHED" is removed and
added to the "MATCHED" group.
In the end, the expression "(?(UNMATCHED)(?!))" can only match if the
"UNMATCHED" group is empty (because (?!) never matches).

I'd suggest commenting out the "(?(UNMATCHED)(?!))" part, and entering this
in Expresso. (http://www12.brinkster.com/ultrapico/Expresso.htm). Test it
against patterns like "(((()", "(((())", "(((()))" or "(())))".

Niki
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top