How to find the location of a substring index matching a regular expression in Perl?

I repeat the file and in every line I am looking for a regular expression. If a regular expression is found, I just want to print “he found” and then the location of the index where it was found on this line.

Example:

looking for: 'HDWFLSFKD' need index between two Ds line: MLTSHQKKF*HDWFLSFKD*SNNYNSKQNHSIKDIFNRFNHYIYNDLGIRTIA output: 'its found' index location: 10-17 

The above “search” is pretty simple, but I plan on having complex regex expressions. So basically just want to know if a regular expression is found in a string, how can we get its location by index?

Here is the code that I still have:

 foreach my $line (@file_data) { if ($line=~ /HDWFLSFKD/){ print "it found\n"; print "but at what index are the two Ds"; } else { $sequence.=$line; print "came in else\n"; } } 
+4
source share
2 answers

I believe you are looking for pos :

  #!/usr/bin/perl use strict; use warnings; my $sequence; while (my $line = <DATA>) { if ($line=~ /(HDWFLSFKD)/g){ print "its found index location: ", pos($line)-length($1), "-", pos($line), "\n"; } else { $sequence .= $line; print "came in else\n"; } } __DATA__ MLTSHQKKF*HDWFLSFKD*SNNYNSKQNHSIKDIFNRFNHYIYNDLGIRTIA MLTSHQKKFSNNYNSKQNHSIKDIFNRFNHYIYNDLGIRTIA MLTSHQKKFSNNYNSK*HDWFLSFKD*QNHSIKDIFNRFNHYIYNDLGIRTIA 

You can also use @- and @+ :

 #!/usr/bin/perl use strict; use warnings; my $sequence; while (my $line = <DATA>) { if ($line=~ /HDWFLSFKD/){ print "its found index location: $-[0]-$+[0]\n"; } else { $sequence .= $line; print "came in else\n"; } } __DATA__ MLTSHQKKF*HDWFLSFKD*SNNYNSKQNHSIKDIFNRFNHYIYNDLGIRTIA MLTSHQKKFSNNYNSKQNHSIKDIFNRFNHYIYNDLGIRTIA MLTSHQKKFSNNYNSK*HDWFLSFKD*QNHSIKDIFNRFNHYIYNDL 
+13
source

You can split the string with a regular expression and print the size of the first element of the array if there are several elements in the array. A simple example:

 my $test="123;456"; my @help=split(';', $test); if ($#help>0) { print "Index is:".length($help[0]); } 

Edit: this matches your simple example, but doesn’t fully match your text - if the regular expression becomes more complex, then the size of the separation criteria becomes flexible again. Then you need to determine the index of the second element of the array to determine the size of the shared criteria.

0
source

All Articles