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


I have a script that reads in lines from a file. One of the lines is this:

@SRR254172.1 ILLUMINA-20A1B2_0004_FC6282EAAXX:6:1:1433:941 length=160

If I use the following test, it fails the test
if ($line2 =~ /^\@\S+\s+ILLUMINA\S+:(\d+):(\d+):(\d+):(\d+)\s+length=1 +60/) { $x2 = $3; $y2 = $4; } else { die "problem: line is $line\n"; #problem: now line is @SRR254172.1 ILLUMINA-20A1B2_0004_FC6282EAAX +X:6:1:1433:941 length=160 }

I think that the problem has to do with the @, but I have escaped the @ in my regular expression. Can someone explain to me how I should match this line with a regular expression that includes @?

Thank you.

Eric

Replies are listed 'Best First'.
Re: trouble with @ in a regular expression
by choroba (Cardinal) on Mar 29, 2012 at 22:06 UTC
    Works for me:
    perl -E 'say q{@SRR254172.1 ILLUMINA-20A1B2_0004_FC6282EAAXX:6:1:1433: +941 length=160} =~ /^@\S+\s+ILLUMINA\S+:(\d+):(\d+):(\d+):(\d+)\s+length= +160/' 611433941
    Maybe your input line contains some invisible characters somewhere?

      I, also, see the match succeed when cut/pasting the text and regex from the OP.

Re: trouble with @ in a regular expression
by JavaFan (Canon) on Mar 29, 2012 at 23:24 UTC
    You regexp seems fine, but your code fragment doesn't populate $line2. Perhaps the problem is there?
Re: trouble with @ in a regular expression
by ww (Archbishop) on Mar 29, 2012 at 23:07 UTC
    perl -e "my $str='foo@bar'; if ($str =~ /@/){ print \"No need to escap +e the \@ in a regex\";}" No need to escape the @ in a regex

    or, more nearly parallel to your problem statement, anchoring the start of the line:

    perl -e "my $str='@bar'; if ($str =~ /^@/){ print \"No need to escape +the \@ in a regex\";}" No need to escape the @ in a regex

      Is too (if it can look like an interpolable variable).

      >perl -wMstrict -le "my $str='foo@bar'; if ($str =~ /@b/){ print 'No need to escape the @ in a regex'; } " Possible unintended interpolation of @b in string at ... Global symbol "@b" requires explicit package name at ... Execution of -e aborted due to compilation errors.
        Good catch! + +

        Note that it's the invocation of strict and warnings (-w) that illuminates my error.