It aborts because you can't have nested quantifiers.
Your expectation of what the "g" modifier does is also wrong. Here is an excerpt from perlretut:
Global matching
The final two modifiers //g and //c concern multiple matches. The modifier //g stands for global matching and allows the matching operator to match within a string as many times as possible. In scalar context, successive invocations against a string will have `//g jump from match to match, keeping track of position in the string as it goes along. You can get or set the position with the pos() function.
The use of //g is shown in the following example. Suppose we have a string that consists of words separated by spaces. If we know how many words there are in advance, we could extract the words using groupings:
1. $x = "cat dog house"; # 3 words
2. $x =~ /^\s*(\w+)\s+(\w+)\s+(\w+)\s*$/; # matches,
3. # $1 = 'cat'
4. # $2 = 'dog'
5. # $3 = 'house'
But what if we had an indeterminate number of words? This is the sort of task //g was made for. To extract all words, form the simple regexp (\w+) and loop over all matches with /(\w+)/g :
1. while ($x =~ /(\w+)/g) {
2. print "Word is $1, ends at position ", pos $x, "\n";
3. }
prints
1. Word is cat, ends at position 3
2. Word is dog, ends at position 7
3. Word is house, ends at position 13
A failed match or changing the target string resets the position. If you don't want the position reset after failure to match, add the //c , as in /regexp/gc . The current position in the string is associated with the string, not the regexp. This means that different strings have different positions and their respective positions can be set or read independently.
In list context, //g returns a list of matched groupings, or if there are no groupings, a list of matches to the whole regexp. So if we wanted just the words, we could use
1. @words = ($x =~ /(\w+)/g); # matches,
2. # $word[0] = 'cat'
3. # $word[1] = 'dog'
4. # $word[2] = 'house'
Reputation Points: 246
Solved Threads: 67
Practically a Posting Shark
Offline 898 posts
since Mar 2006