Can I determine which regex in an either or statement matches my string?

perl, regex

Solution

In scalar context, the `=~` operator returns the number of matches. Without the `/g` modifier, the number of matches is either 0 or 1, so you could do something like

$match_val = ($SDescription =~ m/$sdescription_1/i)
         + 2 * ($SDescription =~ m/$sdescription_2/i);
if ($match_val) {

    if ($match_val == 1) { ... }  # matched first regex
    if ($match_val == 2) { ... }  # matched second regex
    if ($match_val == 3) { ... }  # matched both regex

}

Problem

For this statement: ``` if ($SDescription =~ m/$sdescription_1/gi or $SDescription =~ m/$sdescription_2/gi){ #... } ``` Besides printing `$SDescription` to compare it manually, is it possible to tell which `$SDescription` matched: `$sdescription_1` or `$sdescription_2`?

Original source