Search code examples
regexperl

Escaping $ variable interpolation in a multiline match


On a multiline string, I'm trying to match a line which contains @@ followed by one or more empty or whitespace containing lines, and then a line beginning with a digit. Here's what I tried to use:

/^\@\@$\s*\d.*/m

This turns out to be a problem, because Perl interprets $\ as a variable interpolation. How can I escape that? If I use \$, then it matches the literal $ which is also not what I want.

Example text to be matched:

@@

1: my text

Solution

  • You could use /x.

    / ^ \@\@$ \s*\d.* /xm
    

    Hacks are possible.

    /^\@\@(?:$)\s*\d.*/m
    

    But it doesn't make sense to use zero-width $ here when you can use \n.

    / ^\@\@\n \s*\d.* /xm