http://www.perlmonks.org?node_id=1219107

ovedpo15 has asked for the wisdom of the Perl Monks concerning the following question:

Hey guys, I'm trying to think of a basic regex which checks if a string is a substring of another string but they are not equal.
Example:
my $str1 = "helloworld"; my $str2 = "hello"; my $str3 = "hello";

So the regex should return true for `$str2 =~ $str1` because $str2 is substring of $str1, but it should return false for `$str2 =~ $str3` because they are the same. Which regex should I use?

Replies are listed 'Best First'.
Re: regex and substrings
by Corion (Pope) on Jul 23, 2018 at 14:36 UTC

    You could for example implement the criteria using these two rules:

    1. A string is a true substring of another if the other string starts with something else
    2. A string is a true substring of another if the other string ends with something else

    Then you only need to find out how to construct an "or" condition for regular expressions and find implementations for "starts with", "ends with" and "something else".

    See perlre for ideas on how to implement these.

Re: regex and substrings
by jeffenstein (Friar) on Jul 23, 2018 at 15:57 UTC

    If you just need a check, and it doesn't have to be a regex, then maybe index() and ne would work?

    if( index($str1, $str2) > -1 && $str1 ne $str2 ){ print "$str2 is a substring of $str1\n"; }

    With tests to make sure it works as expected:

    #!/usr/bin/env perl use strict; use warnings; use Test::Simple; my $str1 = "helloworld"; my $str2 = "hello"; my $str3 = "hello"; ok( is_substring($str2, $str1), "str2 is a substring of str1"); ok( !is_substring($str3, $str2), "str3 is not a substring of str2"); sub is_substring { my ($substring, $string) = @_; return 1 if index($string, $substring) > -1 && $string ne $substri +ng; }
Re: regex and substrings
by AnomalousMonk (Bishop) on Jul 24, 2018 at 15:51 UTC

    Implementing the criteria given by Corion here, and just for completness, here's a single-regex solution. Note that this accepts the empty string as a substring of any non-empty string.

    (Should work on any Perl version 5.8.9+.)


    Give a man a fish:  <%-{-{-{-<

A reply falls below the community's threshold of quality. You may see it by logging in.