I think this do not work at cases when '*' is absent or not in the middle!


If '*' is in the middle of string, it seems OK:
#!/usr/bin/perl # http://perlmonks.org/?node_id=1196089 use warnings; use strict; $\ = $/; while(<DATA>){ chomp; my $dict = $_; $_ = <DATA>; my( $head, $tail ) = map length( $_ // '' ), split /\*/, $_; print "head:[$head], tail:[$tail]"; s/\?/[$dict]/g; s/\*/[^$dict]*/; my $qr = qr/^$_$/; print "regex:[$qr]"; (print "loop #:[$_]"), $_ = <DATA>, (print "[$_]"), substr($_, $head, 0, ' '), (print "[$_]"), substr($_, -$tail, 0, ' '), (print "[$_]"), print "Answer: ", $_ =~ $qr ? "YES" : "NO" for 1 .. <DATA>; } __DATA__ ab a?*a 2 aacca aaccb
OUTPUT:
head:[2], tail:[2] regex:[(?^:^a[ab][^ab]*a $)] loop #:[1] [aacca ] [aa cca ] [aa cc a ] Answer: YES loop #:[2] [aaccb ] [aa ccb ] [aa cc b ] Answer: NO
If star is absent, output should contain "YES" and then "NO". Another __DATA__ for same code:
__DATA__ ab a?a 2 aaa aab
OUTPUT
head:[4], tail:[] regex:[(?^:^a[ab]a $)] loop #:[1] [aaa ] [aaa ] [ aaa ] Answer: NO loop #:[2] [aab ] [aab ] [ aab ] Answer: NO


But I understood idea, and after modified a bit. Added line #13, and changed lines #25-26, got OK:
#!/usr/bin/perl # http://perlmonks.org/?node_id=1196089 use warnings; use strict; $\ = $/; while(<DATA>){ chomp; my $dict = $_; $_ = <DATA>; my $star = /\*/; my( $head, $tail ) = map length( $_ // '' ), split /\*/, $_; print "head:[$head], tail:[$tail]"; s/\?/[$dict]/g; s/\*/[^$dict]*/; my $qr = qr/^$_$/; print "regex:[$qr]"; (print "loop #:[$_]"), $_ = <DATA>, (print "[$_]"), $star && substr($_, $head, 0, ' '), (print "[$_]"), $star && substr($_, -$tail, 0, ' '), (print "[$_]"), print "Answer: ", $_ =~ $qr ? "YES" : "NO" for 1 .. <DATA>; } __DATA__ ab a?a 2 aaa aab
OUTPUT:
head:[4], tail:[] regex:[(?^:^a[ab]a $)] loop #:[1] [aaa ] [aaa ] [aaa ] Answer: YES loop #:[2] [aab ] [aab ] [aab ] Answer: NO
But when trying to submit to codeforces, got Run-time error, seems because of star is at the end,
or at beginning of a string (http://codeforces.com/contest/832/submission/28928022 , TC #3).

In reply to Re^2: the case where regex seems to work slower by rsFalse
in thread the case where regex seems to work slower by rsFalse

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, details, 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, summary, 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.