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
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]
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With