Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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.

c:\@Work\Perl\monks>perl -le "use warnings; use strict; ;; use Test::More 'no_plan'; use Test::NoWarnings; ;; use constant SUBSTRINGS => ( 'the empty string is a substring of any non-empty string', [ 'x', '' ], [ 'helloworld', '' ], [ 'xx', 'x' ], [ 'xy', 'x' ], [ 'yx', 'x' ], [ 'yxy', 'x' ], [ 'helloworld', 'hello' ], [ 'helloworld', 'world' ], [ 'hellohello', 'hello' ], [ 'hello world', 'hello' ], [ 'hello world', 'world' ], [ 'hello hello', 'hello' ], [ 'xhelloworldx', 'hello' ], [ 'xhelloworldx', 'world' ], [ 'xhello worldx', 'hello' ], [ 'xhello worldx', 'world' ], ); ;; use constant NO_SUBSTRINGS => ( 'the empty string is not a substring of itself', [ '', '' ], [ '', 'x' ], [ 'x', 'x' ], [ 'x', 'xx' ], [ 'x', 'y' ], [ 'foo', 'bar' ], [ 'hello', 'hello' ], [ 'hello', 'xhello' ], [ 'hello', 'hellox' ], [ 'xhello', 'hellox' ], [ 'hello', 'helloworld' ], ); ;; note 'each of these strings has a substring'; VECTOR_SUBSTRING: for my $ar_vector (SUBSTRINGS) { if (not ref $ar_vector) { note $ar_vector; next VECTOR_SUBSTRING; } ;; my ($string, $substring) = @$ar_vector; ;; ok $string =~ m{ \A .+ \Q$substring\E | \Q$substring\E .+ \z }xms, qq{'$string' contains '$substring'}; } ;; note 'none of these strings has a substring'; VECTOR_NO_SUBSTRING: for my $ar_vector (NO_SUBSTRINGS) { if (not ref $ar_vector) { note $ar_vector; next VECTOR_NO_SUBSTRING; } ;; my ($string, $substring) = @$ar_vector; ;; ok $string !~ m{ \A .+ \Q$substring\E | \Q$substring\E .+ \z }xms, qq{'$string' does not contain '$substring'}; } ;; done_testing; ;; exit; " # each of these strings has a substring # the empty string is a substring of any non-empty string ok 1 - 'x' contains '' ok 2 - 'helloworld' contains '' ok 3 - 'xx' contains 'x' ok 4 - 'xy' contains 'x' ok 5 - 'yx' contains 'x' ok 6 - 'yxy' contains 'x' ok 7 - 'helloworld' contains 'hello' ok 8 - 'helloworld' contains 'world' ok 9 - 'hellohello' contains 'hello' ok 10 - 'hello world' contains 'hello' ok 11 - 'hello world' contains 'world' ok 12 - 'hello hello' contains 'hello' ok 13 - 'xhelloworldx' contains 'hello' ok 14 - 'xhelloworldx' contains 'world' ok 15 - 'xhello worldx' contains 'hello' ok 16 - 'xhello worldx' contains 'world' # none of these strings has a substring # the empty string is not a substring of itself ok 17 - '' does not contain '' ok 18 - '' does not contain 'x' ok 19 - 'x' does not contain 'x' ok 20 - 'x' does not contain 'xx' ok 21 - 'x' does not contain 'y' ok 22 - 'foo' does not contain 'bar' ok 23 - 'hello' does not contain 'hello' ok 24 - 'hello' does not contain 'xhello' ok 25 - 'hello' does not contain 'hellox' ok 26 - 'xhello' does not contain 'hellox' ok 27 - 'hello' does not contain 'helloworld' 1..27 ok 28 - no warnings 1..28
(Should work on any Perl version 5.8.9+.)


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


In reply to Re: regex and substrings by AnomalousMonk
in thread regex and substrings by ovedpo15

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others chilling in the Monastery: (2)
    As of 2021-03-05 04:47 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      My favorite kind of desktop background is:











      Results (109 votes). Check out past polls.

      Notices?