Rob Rob - 1 year ago 48
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!

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download