Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: regex and substrings

by sundialsvc4 (Abbot)
on Jul 23, 2018 at 18:29 UTC ( #1219128=note: print w/replies, xml ) Need Help??


in reply to regex and substrings

How about (Perl >= 5.16): (you could also use \Q but I think that’s more cryptic ...)   See perldoc quotemeta ...

use strict; use warnings; my $str1 = "helloworld"; my $str2 = "hello"; my $meta1 = quotemeta($str1); // to auto-escape special chars my $meta2 = quotemeta($str2); if (($str1 ne $str2) && (($str1 =~ /$meta2/) || ($str2 =~ /$meta1/)) +) { print "yes\n"; } yes

Nonetheless, I think I would vote for the simple index-based strategy cited earlier because it will be much more familiar to programmers versed in other languages, and it will do the job just as well.

Replies are listed 'Best First'.
Re^2: regex and substrings
by marto (Archbishop) on Jul 23, 2018 at 18:43 UTC

    Doesn't even compile:

    Bareword found where operator expected at derp.pl line 7, near "// to" (Missing operator before to?) "my" variable $str2 masks earlier declaration in same scope at derp.pl + line 8. "my" variable $str1 masks earlier declaration in same statement at der +p.pl line 9. "my" variable $str2 masks earlier declaration in same statement at der +p.pl line 9. syntax error at derp.pl line 7, near "// to auto" Execution of derp.pl aborted due to compilation errors.

    You pontificate about the language about testing, you don't even know how to comment perl code. This is not the first time you've done this.

Re^2: regex and substrings
by AnomalousMonk (Bishop) on Jul 23, 2018 at 19:43 UTC
    ... (Perl >= 5.16) ...

    Ignoring the fact that I don't understand the meaning of the quoted bit, and sidestepping the foreign comment operator, it compiles and does what ovedpo15 seems to want. On the whole, a bucketful out of the boat, so an upvote from me. (Now please, please don't negate this with a dozen bucketsful back into the boat!)


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

      I know you're just trying to help; I think it's water on a duck. What ensures the downvote from me is that he could edit the node to fix the comment and mark it with (Update: fixed comment character). It's trivial to improve the monastery with these tiny bits of effort. It's what us self-important pricks do on a daily basis.

        ... water on a duck.

        Sadly, I'm starting to agree.


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

Re^2: regex and substrings
by Marshall (Abbot) on Jul 24, 2018 at 19:21 UTC
    I like the solution from jeffenstein++

    Regex will be slower than the above solution.
    I would code a regex version like this:

    #!/usr/bin/perl use strict; use warnings; my $str1 = "helloworld"; my $str2 = "hello"; if ($str1 ne $str2 and $str1 =~ /\Q$str2\E/) { print "yes, $str2 is a substring of $str1\n"; } # Note: the \Q and \E escapes are not needed in # this exact situation. These escape characters mean # to ignore any chars in $str2 that might otherwise # mean something to the regex engine. # In this simple case the \E is not needed, but # I would recommend a \Q...\E pair. __END__ Prints: yes, hello is a substring of helloworld

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1219128]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2019-12-12 08:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?