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
?
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
}
Is there a reason why you don't want to write something like this? (The /g
modifier is superflous unless you are using the \G
anchor in a later match: a pattern either matches or it doesn't.)
if ($SDescription =~ /$sdescription_1/i) {
# Do stuff for first pattern
}
elsif ($SDescription =~ /$sdescription_2/i) {
# Do stuff for second pattern
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With