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

Dear Monks, I am trying to grep for a chunk of lines in a text file but it does not seem to work:

FINAL PREDICTION ================ 1: (out) 10-33 (10.82) 10 20 30 40 50 60 SSSSSSSS-OOOOOOXXXXXXXXXXXXIIIIII+++++++++++++++++++++++++++ MSSPLRMDVTFLLAAIAVTWVCGLKIGFPGFSTPPRSFIQHPKRTLCPEDCDIASPFKCE 70 80 90 100 110 120 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ESPTCLRLFQVCNGRWDCEHGSDEDNALCAAVLRPLECMIWEFLEGQRDWILPNLFNDAN 130 140 150 160 170 180 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ TDLVAHALHEAYSMGDLQSYLNLTDQNIENIRNSTRGAIVGDPRPLMALGMPDRAWPEVM 190 ++++++++++++++++ YLLKELYNLGLDVWAE

and I only need to grep the lines that contain S,I,O,X,- and + (basically the middle lines in every 3 lines. I tried this:
$output=''; while($_=~/\A([S\-\+XIO]+)\z/g) { $part=$1; $output=$output.$part; } print "$output\n";

it does not work. The desired output would be:
SSSSSSSS-OOOOOOXXXXXXXXXXXXIIIIII+++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Replies are listed 'Best First'.
Re: Pattern matching fails because of special characters
by AnomalousMonk (Archbishop) on Jun 10, 2019 at 01:15 UTC

    Where and with what is  $_ being assigned? If  $_ does not match the pattern, the while loop will never be entered. If  $_ does match, the while loop will be infinite.

    c:\@Work\Perl\monks>perl -wMstrict -le "$_ = 'no joy'; ;; my $output = ''; while($_ =~ /\A([S\-\+XIO]+)\z/g) { my $part = $1; $output = $output . $part; } print qq{final output: '$output'}; " final output: ''
    Please see Short, Self-Contained, Correct Example.

Re: Pattern matching fails because of special characters
by LanX (Saint) on Jun 10, 2019 at 00:18 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.

Re: Pattern matching fails because of special characters
by stevieb (Canon) on Jun 09, 2019 at 23:31 UTC

    It would be prudent and helpful if you could show us what you're currently getting back, and how it's wrong. Edit your Original Post with that information.