in reply to Pattern matching fails because of special characters

Hi

>  while($_=~/\A([S\-\+XIO]+)\z/g)

You can't escape with backslashes inside a character class. °

Put the minus at the end and drop all backslashes, unless you want to match '\'

See also Re: regex question Underscores, lines and paratheses.

HTH! :)

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

°) thanks to AnomalousMonk++ for correcting me!

Replies are listed 'Best First'.
Re^2: Pattern matching fails because of special characters
by AnomalousMonk (Archbishop) on Jun 10, 2019 at 01:02 UTC
    You can't escape with backslashes inside a character class.
    Put the minus at the end and drop all backslashes, unless you want to match \ .

    A counterexample:

    c:\@Work\Perl\monks>perl -wMstrict -le "my $s = 'abc\ZD+A-G\HJxyz'; print qq{'$s'}; ;; $s =~ m{ ([A\-Z\+GD]+) }xms; print qq{'$1'}; " 'abc\ZD+A-G\HJxyz' 'ZD+A-G'
    (But I agree that putting the - at the end and dropping the backslashes would have the same effect.)


    Give a man a fish:  <%-{-{-{-<

      you are right

      perlrecharclass#Special-Characters-Inside-a-Bracketed-Character-Class

      Characters that may carry a special meaning inside a character class are: \ , ^ , - , and , and are discussed below. They can be escaped with a backslash, although this is sometimes not needed, in which case the backslash may be omitted.

      Mea culpa!

      (time to retire? :)

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

      PS: I'm afraid perldoc.perl.org "improved" to the worse.