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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |