Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^4: Problem with regex wildcard operator (.)...?

by AnomalousMonk (Archbishop)
on Sep 06, 2021 at 03:04 UTC ( [id://11136487]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Problem with regex wildcard operator (.)...?
in thread Problem with regex wildcard operator (.)...?

... 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:  <%-{-{-{-<

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-04-19 03:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found