Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
... it has something to do with the regex order ...

The regexes actually being used are all anchored with ^ $ assertions (see perlre, perlretut). That means that certain characters have to appear at the beginning or end of certain strings for a match to occur.

I'm also unsure of just what you expect to get, but consider this code:

Win8 Strawberry 5.8.9.5 (32) Sun 09/05/2021 22:17:31 C:\@Work\Perl\monks >perl -Mstrict -Mwarnings use Data::Dump qw(dd); my @words = ( # added a few extra 'words' '', 'x', 'a', 's', 'aa', 'as', 'es', 'is', 'os', 'sh', 'si', 'so' ); dd 'WORDS:', \@words; print "----------------------------\n"; for my $regex (".?a?", "a?.?", "s?.?", ".?s?",) { print "Matches for ACTUAL regex pattern : ^$regex\$\n\n"; foreach my $word (@words) { my $sorted = join '', sort split //, $word; if ($sorted =~ /^$regex$/) { print "'$sorted' -> '$word' "; } } print "\n----------------------------\n"; } ^Z ( "WORDS:", ["", "x", "a", "s", "aa", "as", "es", "is", "os", "sh", "si", "so"], ) ---------------------------- Matches for ACTUAL regex pattern : ^.?a?$ '' -> '' 'x' -> 'x' 'a' -> 'a' 's' -> 's' 'aa' -> 'aa' ---------------------------- Matches for ACTUAL regex pattern : ^a?.?$ '' -> '' 'x' -> 'x' 'a' -> 'a' 's' -> 's' 'aa' -> 'aa' 'as' -> 'a +s' ---------------------------- Matches for ACTUAL regex pattern : ^s?.?$ '' -> '' 'x' -> 'x' 'a' -> 'a' 's' -> 's' ---------------------------- Matches for ACTUAL regex pattern : ^.?s?$ '' -> '' 'x' -> 'x' 'a' -> 'a' 's' -> 's' 'as' -> 'as' 'es' -> 'e +s' 'is' -> 'is' 'os' -> 'os' 'hs' -> 'sh' 'is' -> 'si' 'os' -> ' +so' ----------------------------
Note that I've added '' (empty string) 'x' 'a' 's' to the @words array. Note also that all the regexes in question match all these new strings.

The complete regex ^.?a?$ matches any zero- or one-character string. The regex can match some two-character strings. For such a match, an 'a' must be at the absolute end of the string (or before a newline at the end of the string). There's only one two-character string in @words that, after the string is sorted, matches this regex: 'aa'. (Update: Neither this regex nor any of the others discussed can match a string of more than two characters.)

The regex ^a?.?$ matches any zero- or one-character string and some two-character strings. For a two-character match, an 'a' must be at the absolute beginning of the string. There are two, two-character strings in @words that, after being sorted, match this regex: 'aa' 'as'.

The regex ^s?.?$ doesn't match any two-character strings because no string in @words, after being sorted, begins with an 's'.

The regex ^.?s?$ matches almost every two-character string in @words because almost every such string, after being sorted, ends in 's'.


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


In reply to Re^4: Problem with regex wildcard operator (.)...? by AnomalousMonk
in thread Problem with regex wildcard operator (.)...? by sbrothy

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2024-04-25 13:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found