Wiggins has asked for the wisdom of the Perl Monks concerning the following question:
Mu conclusion is that 'optional' success ('?'="zero or one") is not a 'match' as far as '(...)' collection is concerned:
results in$m = ' more gibberish"h" \n URL="http://[10.0.0.3]?id=80943lkjh875kjrv +f09u548gfpi"\n gibber\n'; $u= qr/(:?URL="([^"]*)")?/is; # optional clause (:?xxx)? if ($m =~ $u) { print "<$1><$2>\n"; }else{ print "no match u\n";}
This result is the same, even if I edit "URL=..." to "URxL=...".[tmp]> perl testResp2.pl <><> [tmp]>
------------- Update-----------
So, these optional expressions "have no legs". They are tested at the current pos() in the target where they are found. In my case that is at the 1st space character at the start of the string.
In order to force the evaluation to move down the target, I tried prepending .*? or .* and finally settled on
But, of course, this is too contrived to be useful. There could be a 'U' anywhere that matches the first part.$u= qr/.*(?=U)(?:URL="([^"]*)")?/s # moves to just before 'U'
Thanks all.
-------------Update 2-----------------
The following will find what I want, ignoring random 'U's.
This positions to 'U' and tries the match, if it fails it moves on. The reason I am stuck on this is because my original regex would become$m = ' more gib U berish"h" \n URL="http://[10.0.0.3]?id=80943lkjh875k +jrvf09u548gfpi"\n gibber\n'; $u= qr/(?:.*(?=U)(?:URL="([^"]*)")?)*/s; #lookahead to 'U' if ($m =~ $u) { print "<$1>\n"; }else{ print "no match u\n";}
This will extract the response id($1), the disposition ($2)(delete|hold|fetch) and an optional URL (only with fetch); from a large message string.$p= qr/RESPONSE\sid="([^"]+?)" .* #random XML disposition="([^"]+?)" .* #random XML (?:.*(?=U)(?:URL="([^"]+?)")?)* /sx;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Regex: succeeds, but parens don't collect...
by kyle (Abbot) on Aug 19, 2008 at 14:18 UTC | |
|
Re: Regex: succeeds, but parens don't collect...
by jhourcle (Prior) on Aug 19, 2008 at 14:30 UTC | |
|
Re: Regex: succeeds, but parens don't collect...
by JavaFan (Canon) on Aug 19, 2008 at 14:29 UTC | |
|
Re: Regex: succeeds, but parens don't collect...
by Anonymous Monk on Aug 19, 2008 at 14:17 UTC | |
|
Re: Regex: succeeds, but parens don't collect...
by Bloodnok (Vicar) on Aug 19, 2008 at 15:03 UTC |