ewhitt has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to match strings where the first char is a ";". I thought this would do the trick, but it isn't:
if ($line !~ m{^;$})

Replies are listed 'Best First'.
Re: How to match a ;
by GrandFather (Saint) on Feb 08, 2009 at 08:45 UTC

    That matches strings where the only character on the line is a ';'. To match just the first character where you don't care what follows, omit the $.


    Perl's payment curve coincides with its learning curve.
      The regex of the OP will evaluate true for all strings except a string consisting of a single ';' character optionally followed by a newline because  $line !~ m{^;$} uses the  !~ negating binding operator.

      Update: corrected per GrandFather's reply below.

        Argh! Quite right. It must be the heat here!

        Well, any string except one containing a single ';' optionally followed by a \n.


        Perl's payment curve coincides with its learning curve.
Re: How to match a ;
by ELISHEVA (Prior) on Feb 08, 2009 at 08:52 UTC

    You also need to make sure that $line is clean of final new lines whitespace, otherwise your regex won't match your line because $line will end in a new line whitespace instead of ';' - see http://perldoc.perl.org/functions/chomp.html. Also you might consider /^;\s*$/ to strip trailing whitespace.

    Syntax wise, your regex is fine.

    Best, beth

    Update:Correction (see jwkrahn and Grandfather below).

      Actually no. $ matches an end of line, which may be a \n character. Consider:

      my $line = ";\n"; print $line =~ /$_/ ? "" : "no ", "match: $_\n" for '^;$', '^;', "^;\\ +z";

      Prints:

      match: ^;$ match: ^; no match: ^;\z

      Note that \z matches the physical end of the string.


      Perl's payment curve coincides with its learning curve.
        The  $ regex metacharacter will "[m]atch the end of the line (or before newline at the end)" (emphasis added, perlre section Regular Expressions).

        In the presence of the /m regex modifier, the behavior of  $ is enhanced.

        I stand corrected, but it was actually '.' and '$' new line behavior I was confusing. I once spent a great deal of time beating my brains out until I figured out that '.' doesn't match \n unless you use the 's' modifier, e.g. /;./s. But the more I think about it, I also am finding '$' less clearer than I thought, but I'll save those questions for another thread.

        Best, beth

        Update 2: removed update 1, appears I still have '$' questions and will need to go to SOPW

        I'm puzzled as to why you switched to double quotes and then had to escape the backslash in your third pattern. Sticking to single quotes or using qw{ ... } would have obviated that need.

        $ perl -le ' > $line = qq{;\n}; > print > $line =~ m{$_} > ? q{} > : q{no }, > qq{match $_} > for q{^;$}, q{^;}, q{^;\z};' match ^;$ match ^; no match ^;\z $ perl -le ' $line = qq{;\n}; print $line =~ m{$_} ? q{} : q{no }, qq{match $_} for qw{ ^;$ ^; ^;\z };' match ^;$ match ^; no match ^;\z $

        Cheers,

        JohnGG

      The $ anchor will match the end of the line with or without a newline at the end so /^;$/ will match both ";" and ";\n".