pattern matching - RegEx: grouping returns only the last match -


i've set of strings in form:

nooo (2), { aaa (1), bbb (2), ccc-cc (3), ddd (4) } 

(elements can more 4 inside brackets)

i need match contents inside brackets , extract (using groups) 'aaa', 'bbb', ... substrings. result example be

group1 : aaa group2 : bbb group3 : ccc-cc group4 : ddd 

i tried expression:

\{ (?:(\s+) \(\d+\),?\s?)+ \} 

but returns last matched group (so, in case, 'ddd'). missing? thanks

if using .net regex expression work capturing group capture values. otherwise have use more tricky regex or match in 2 steps, first matching { ... } group , elements in it.

the tricky regex like:

(?:{|\g(?!^),)   # match { or previous match ended followed , \s+              # space between elements (\s+)\s+\(\d+\)  # element (?=[^{]*})       # make sure it's followed } 

you can use expression it's written if use /x flag (can set adding (?x) in beginning of expression).

the regex without comments:

(?:{|\g(?!^),)\s+(\s+)\s+\(\d+\)(?=[^{]*}) 

this expression uses \g regex flavor has support. modern regex flavors have it, including: perl, pcre (php/etc), .net.

note such expression isn't perfect. capture aaa , bbb in following string example:

{ aaa (1), bbb (23), ccc, invalid here #¤% ))),,,,!! } 

altho can fixed if required (except counter).


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -