Rob Rob - 3 months ago 7x
Perl Question

Regular expression search with only a portion of a variable

if ($Sequence -~ m/$BestMatch/){

Is there a way to modify the regex to only search for the first 6 letters in the $BestMatch sequence? I need to keep the $BestMatch variable in place in the regex, I just need to modify it.

The examples I find online do not make use of a variable, but rather the entire sequence itself. My problem is that the $BestMatch will change for every file opened in a foreach loop, and therefore can't be hardcoded.
Thanks for the help!


Try something like :

my$substr = substr($BestMatch,0,6);

if ($Sequence -~ m/\Q$substr/){ 

    # ... whatevs ....


Of course, if it's just a simple match of strings, you don't need t even use regex; just use the index() function.