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?? |
... 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: 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 Section
Seekers of Perl Wisdom
|
|