对于这个声明:
if ($SDescription =~ m/$sdescription_1/gi or $SDescription =~ m/$sdescription_2/gi){ #... }
除了打印$SDescription手动比较它,是否可以分辨出哪个$SDescription匹配:$sdescription_1或$sdescription_2?
解决方法@H_404_17@
在标量上下文中,=〜运算符返回匹配数.没有/ g修饰符,匹配的数量是0或1,所以你可以做类似的事情
$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
}
$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 }