简体   繁体   中英

Perl regular expression built from values in variables

Given the variables:

my $var1 = "fish";
my $var2 = "ocean";

My input strings can be either fishin_ocean or fishout_ocean .

How do I write a Perl regex to match both the input strings, where parts of the regex are in $var1 and $var2 ? This one (where the string is in $var3 ) doesn't return either of the above strings:

  $var3 =~ /$var1.+?$var2/;

Edit from OP's answer:

I think it is the special square braket at the end that is causing the problem

    my $a = "slash_wideiscan"; my $b = "_mnn3h_reg[0]";
    my @array1 = qw (slash_wideiscanps_mnn3h_reg[0] 
                     slash_wideiscanptlr_mnn3h_reg[0] 
                     slash_wideiscanps_mnn0h_reg);
    for my $element (@array1) {
        if ($element =~ /($a.+?$b)/) {
            print "YES : $element\n";
        } else {
            print "NO : $element\n";
        }
    }

Answer that came back was

    NO : slash_wideiscanps_mnn3h_reg[0]
    NO : slash_wideiscanptlr_mnn3h_reg[0]
    NO : slash_wideiscanps_mnn0h_reg

I actually want

    YES : slash_wideiscanps_mnn3h_reg[0]
    YES : slash_wideiscanptlr_mnn3h_reg[0]
    NO : slash_wideiscanps_mnn0h_reg

Not quite clear what you are looking for, but here is your regex in action:

my $fish  = "fish";
my $ocean = "ocean";

for my $word ("fishin_ocean", "fishout_ocean") {
    if ($word =~ /($fish.+?$ocean)/) {  # parens capture to $1
         print "Word $word matches\n";
         print "The matching part is: $1\n";  # $1 contains the captured text
    }
}

More info in perldoc perlretut and perldoc perlre

这应该工作:

/fish(in|out)_ocean/

You regex is fine. The problem is that you are thinking the regex returns the matched string, but in reality, it only returns true or false (if it matches or does not match).

If you want to capture a match, use parentheses around the part you want to capture. Captures are stored in order in $1, $2, $3, etc...

my ($var1, $var2, $var3) = ("fish", "ocean", "fishin_ocean")

$var3 =~ /($var1)(.+?$var2)/;    # only returns true or false
print $1;                        # prints fish
print $2;                        # prints in_ocean

According to your answer (that must be an update to your question), escape each part of your regex with \\Q\\E like the following:

my $a = "slash_wideiscan"; 
my $b = "_mnn3h_reg[0]";
my @array1 = qw (slash_wideiscanps_mnn3h_reg[0] 
                 slash_wideiscanptlr_mnn3h_reg[0] 
                 slash_wideiscanps_mnn0h_reg);
for my $element (@array1) {
    if ($element =~ /(\Q$a\E.+?\Q$b\E)/) {
               here___^   ^    ^   ^
        print "YES : $element\n";
    } else {
        print "NO : $element\n";
    }
}

output:

YES : slash_wideiscanps_mnn3h_reg[0]
YES : slash_wideiscanptlr_mnn3h_reg[0]
NO : slash_wideiscanps_mnn0h_reg

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM