I have come to an empirical conclusion, and am seeking (?confirmation|judgement). The following is a minimal case. I was looking to regex match 2 fields, plus an optional 3rd from a string and extract the possibly 3 matches with paren collection.

Mu conclusion is that 'optional' success ('?'="zero or one") is not a 'match' as far as '(...)' collection is concerned:

$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";}
results in
[tmp]> perl testResp2.pl <><> [tmp]>
This result is the same, even if I edit "URL=..." to "URxL=...".
The evaluation is 'successful' but there was really no 'match' (or a match happened 0 times which is valid). Can I force a match to happen here 1 time (while keeping it optional)?

------------- 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

$u= qr/.*(?=U)(?:URL="([^"]*)")?/s # moves to just before 'U'
But, of course, this is too contrived to be useful. There could be a 'U' anywhere that matches the first part.
This probably generalizes to *, and {0,n} as well.

Thanks all.

-------------Update 2-----------------
The following will find what I want, ignoring random 'U's.

$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 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
$p= qr/RESPONSE\sid="([^"]+?)" .* #random XML disposition="([^"]+?)" .* #random XML (?:.*(?=U)(?:URL="([^"]+?)")?)* /sx;
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.
And it is an intriguing puzzle.

In reply to Regex: succeeds, but parens don't collect... by Wiggins

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.