我需要确定是否
+review CR @someuser
存在于字符串中。我怎样才能做到这一点?
您将需要转义元字符并使用匹配的正则表达式m//
。
if ($string =~ /\+review CR \@someuser/) {
# do something
}
请注意,您不能使用\Q ... \E
转义序列来转义元字符,因为@someuser
仍会被插值。您可以将其用于+
,但您仍然需要转义@
,因此这种方式更简单。您也可以使用该quotemeta
功能。但是,在这种情况下,这可能是矫枉过正。
阅读更多关于此的内容perldoc perlop
Use index
:
$search_string = "+review CR \@someuser";
if (index($string, $search_string) != -1) { # found }
Or, if you use a regex, you'll want to make sure the '+' and '@' are properly escaped:
if ( $string =~ m#\+review CR \@someuser# ) {
# found
}