Perl - If string contains text?
Solution 1:
If you just need to search for one string within another, use the index
function (or rindex
if you want to start scanning from the end of the string):
if (index($string, $substring) != -1) {
print "'$string' contains '$substring'\n";
}
To search a string for a pattern match, use the match operator m//
:
if ($string =~ m/pattern/) { # the initial m is optional if "/" is the delimiter
print "'$string' matches the pattern\n";
}
Solution 2:
if ($string =~ m/something/) {
# Do work
}
Where something
is a regular expression.