Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

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

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

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

    Doesn't even compile:

    Bareword found where operator expected at line 7, near "// to" (Missing operator before to?) "my" variable $str2 masks earlier declaration in same scope at + line 8. "my" variable $str1 masks earlier declaration in same statement at der line 9. "my" variable $str2 masks earlier declaration in same statement at der line 9. syntax error at line 7, near "// to auto" Execution of 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 (Canon) 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?

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (4)
As of 2021-09-19 09:24 GMT
Find Nodes?
    Voting Booth?

    No recent polls found