Making a parenthesized pattern optional with ? is causing the match to fail and I don't understand why.

What I thought: adding a ? after the parens would make the match optional, but if the match was there, I would definitely see it

What seems to me to be happening: adding a ? after the parens says "if there's anyway to not see this match, skip it"...or something. I don't understand.

In the debug session below, first I set up the string to contain nothing but the thing I'm looking for, 'a=b'. If that's all that is in the string, it matches with or without the enclosing parens and question mark.

Then I add an arbitrary extra character ("x") before what I'm trying to match, and the ? seems to say "optional? you bet, I'll definitely take the option of not matching that and just not see your optional thing".

DB<19> $foo = 'a=b'; DB<20> x $foo =~/a=([a-z]+)/; 0 'b' DB<21> x $foo =~/(?:a=([a-z]+))?/; 0 'b' DB<22> $foo = 'x' . $foo DB<23> x $foo =~/(?:a=([a-z]+))?/; 0 undef

I've simplified this to smallest form, but let me give context. I have a very normal situation of something that should always match at the beginning of the line and then there will also sometimes be this "a=b" type construct later. So I wanted to optionally also match the a=b construct. So the real pattern I'm matching is more like (untested)

/(\d\d\d\d-\d\d-\d\d) .*?(?:a=([a-z]+))?/

This really seemed to me like a straightforward use of optional match of a sub expression, and I'm completely baffled and feel stupid.

What should I have done here to have something always match that date and occasionally also have the variable assignment later in the string?


In reply to Making a subpattern optional with ? causes subpattern match to fail. I am confused as to why. by msouth

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.