134

How can I check whether a given string contains a certain substring, using Perl?

More specifically, I want to see whether s1.domain.example is present in the given string variable.

0

3 Answers 3

261

To find out if a string contains substring you can use the index function:

if (index($str, $substr) != -1) {
    print "$str contains $substr\n";
} 

It will return the position of the first occurrence of $substr in $str, or -1 if the substring is not found.

2
  • 8
    This way is especially preferrable, when you are searching using a variable - this way you won't have to double-escape characters (in this variable string), that are special for regular expressions (like :).
    – evgeny9
    Apr 2, 2013 at 14:10
  • See this as well - troubleshooters.com/codecorn/littperl/perlreg.htm
    – Sam B
    Dec 7, 2020 at 19:45
67

Another possibility is to use regular expressions which is what Perl is famous for:

if ($mystring =~ /s1\.domain\.example/) {
   print qq("$mystring" contains "s1.domain.example"\n);
}

The backslashes are needed because a . can match any character. You can get around this by using the \Q and \E operators.

my $substring = "s1.domain.example";
    if ($mystring =~ /\Q$substring\E/) {
   print qq("$mystring" contains "$substring"\n);
}

Or, you can do as eugene y stated and use the index function. Just a word of warning: Index returns a -1 when it can't find a match instead of an undef or 0.

Thus, this is an error:

my $substring = "s1.domain.example";
if (not index($mystring, $substr)) {
    print qq("$mystring" doesn't contains "$substring"\n";
}

This will be wrong if s1.domain.example is at the beginning of your string. I've personally been burned on this more than once.

4
  • I suppose index() is faster than regexps in the simple case?
    – G. Cito
    Feb 25, 2016 at 0:16
  • 1
    An attempted perl answer to a question about removing substrings - I did not compare speed but =~, index(), ~~ and match::simple all seemed a bit awk-ward ... :-\
    – G. Cito
    Feb 25, 2016 at 0:20
  • You also have to make sure that the strings that you are comparing is not case sensitive. S1.DOMAIN.COM will not work for substring. But if you lc($givendomain), and then compare that with "s1.domain.com", then that will work. Also, substring is not necessarily the right approach - see my note above to eugene y's response Nov 10, 2016 at 20:51
  • @David W. Somehow I can't get domain.com to match subdomain.domain.com using the REGEX above, any ideas ?
    – avrono
    Nov 15, 2016 at 11:05
16

Case Insensitive Substring Example

This is an extension of Eugene's answer, which converts the strings to lower case before checking for the substring:

if (index(lc($str), lc($substr)) != -1) {
    print "$str contains $substr\n";
} 
1
  • 3
    Note that fc is recommended over lc for case-insensitive comparisons.
    – melpomene
    Sep 2, 2019 at 11:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.