if I have a input with new lines in it like:
[INFO]
xyz
[INFO]
How can I pull out the xyz part using $ anchors? I tried a pattern like /^\[INFO\]$(.*?)$\[INFO\]/ms, but perl gives me:
Use of uninitialized value $\ in regexp compilation at scripts\t.pl line 6.
Is there a way to shut off interpolation so the anchors work as expected?
EDIT: The key is that the end-of-line anchor is a dollar sign but at times it may be necessary to intersperse the end-of-line anchor through the pattern. If the pattern is interpolating then you might get problems such as uninitialized $\. For instance an acceptable solution here is /^\[INFO\]\s*^(.*?)\s*^\[INFO\]/ms but that does not solve the crux of the first problem. I’ve changed the anchors to be ^ so there is no interpolation going on, and with this input I’m free to do that. But what about when I really do want to reference EOL with $ in my pattern? How do I get the regex to compile?
The question is academic–there’s no need for the
$anchors in your regex anyway. You should be using\nto match the newlines, because the$only matches the gap between the linefeed and the character before it.EDIT: What I’m trying to say is that you will never need to use
$that way. Any match that spans from one line to the next will have to consume the line separator somehow. Consider your example:If this did compile, the
(.*?)would start out by consuming the first linefeed and keep going until it had matched\nxyz, where the second$would succeed. But the next character is a linefeed, and the regex is looking for[, so that doesn’t work. After backtracking, the(.*?)would reluctantly consume one more character–the second linefeed–but then the$would fail.Any time you try to match an EOL with
$and then some more stuff, the first “stuff” you’ll have to match will be the linefeed, so why not match that instead? That’s why the Perl regex compiler tries to interpret$\as a variable name in your regex: it makes no sense to have an end-of-line anchor followed by a character that’s not a line separator.