I had to fight with this a lot to get a "true" output. And even then, I cheated. The most basic of the problems is that ^ is a zero-width assertion. Think about another zero-width assertion, \b. That is, the break between alphanumeric and non-alphanumeric. If you have /a\bc/, this can never match anything because there is not, by definition, a change between word and non-word between an a and a c. Can't happen. Similarly, ^ is a zero-width assertion that asserts this is the beginning of a line. Some pedants may point out that it actually is the beginning of the string, but that's not quite true. The m modifier allows ^ to match anywhere in the string - in fact, according to perlre, the m modifier is merely removing the optimisation that perl has that assumes there is only one line in the string you're testing. That means that it's assuming it's a single line, thus ^ is the beginning of the string because of the assumption there is only one line.
Anyway, ^, being zero-width, must be right after either the physical beginning of the string, or right after a \n. It can't be right after a quote.
However, if we insert a \n into your regex right before the ^, we still don't quite get it to work because you're missing the m modifier. I'm also assuming you haven't set the deprecated $* variable (see perlvar, but don't use it - it's deprecated). Let's say we use the m modifier. It still doesn't work because $_=<> will only drag in a single line. Typing in "\nabc... won't match because $_ will only have the ", terminating the input on the carriage return. There is more cheating to be had: adding local $/; before the input line. Now I have:
And, lo and behold, it works. But notice: I added the $/ and $* (which you shouldn't do) variables, and the \n inside your regex.(echo '"'; echo "abcd") | perl -le 'local $/;$_=<>; $*=1; print "[$_]" +; if (/"\n^abc/) { print "true" }'
I do have to wonder, though, why you're asking this question. It has a slight odor of XY Problem ... or maybe homework. But only slight.
In reply to Re: What it mathches`
by Tanktalus
in thread What it mathches`
by abubacker
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |