Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression in index function

I am looking for occurrence of "CCGTCAATTC(A|C)TTT(A|G)AGT" in a text file.

$text = 'CCGTCAATTC(A|C)TTT(A|G)AGT'; if ($line=~/$text/){ chomp($line); $pos=index($line,$text); }

Searching is working, but I am not able to get the position of "text" in line. It seems index does not accepts a regular expression as substring.

How can I make this work. Thanks

like image 542
Deep Avatar asked Sep 11 '11 22:09

Deep


2 Answers

The @- array holds the offsets of the starting positions of the last successful match. The first element is the offset of the whole matching pattern, and subsequent elements are offsets of parenthesized subpatterns. So, if you know there was a match, you can get its offset as $-[0].

like image 178
Michał Wojciechowski Avatar answered Sep 28 '22 08:09

Michał Wojciechowski


You don't need to use index at all, just a regex. The portion of $line that comes before your regex match will be stored in $` (or $PREMATCH if you've chosen to use English;). You can get the index of the match by checking the length of $`, and you can get the match itself from the $& (or $MATCH) variable:

$text = 'CCGTCAATTC(A|C)TTT(A|G)AGT';
if ($line =~ /$text/) {
    $pos = length($PREMATCH);
}

Assuming you want to get $pos to continue matching on the remaining part of $line, you can use the $' (or $POSTMATCH) variable to get the portion of $line that comes after the match.

See http://perldoc.perl.org/perlvar.html for detailed information on these special variables.

like image 31
Mansoor Siddiqui Avatar answered Sep 28 '22 08:09

Mansoor Siddiqui