Here is a solution that detects when initial alphas of separate strings are the same regardless of case, and that I think is immune to differences in the locale or the Unicode character set that may be in effect. My understanding of the effects of locale and Unicode on regex behavior continues to be a bit vague.
I would be interested in any comments, links, etc., from other monks on my notions that:
- The regex expression [^\W\d_] matches an alpha character regardless of locale or the ASCII or Unicode character set in effect during compilation of the script.
- The expressions [[:alpha:]] and [^\W\d_] have exactly the same behavior.
- The //o modifier of the m{ \A ([^\W\d_]) }xmso regex prevents re-compilation of the regex on multiple calls and so is, in theory, more efficient.
- After two strings in which the first characters are identical have been bitwise-xored in the expression ((lc str1) ^ (lc str2)) =~ /^\0/, the regex /^\0/ is not guaranteed always to match with the first character of the resultant string regardless of locale or character set.
>perl -wMstrict -le
"print '----- output -----';
while (my ($word1, $word2) = splice @ARGV, 0, 2) {
print qq{:$word1: and :$word2: initial letters },
first_letter_matches($word1, $word2)
? 'same' : 'different';
}
sub first_letter_matches {
my ($w1, $w2) = @_;
return $w1 =~ m{ \A ([^\W\d_]) }xmso
&& $w2 =~ m{ \A $1 }xmsi
;
}
"
a a a A Ab abc b b abcd A A1% a2#
"" "" "" a a "" a b a bc ab c 123 123 %# %#
----- output -----
:a: and :a: initial letters same
:a: and :A: initial letters same
:Ab: and :abc: initial letters same
:b: and :b: initial letters same
:abcd: and :A: initial letters same
:A1%: and :a2#: initial letters same
:: and :: initial letters different
:: and :a: initial letters different
:a: and :: initial letters different
:a: and :b: initial letters different
:a: and :bc: initial letters different
:ab: and :c: initial letters different
:123: and :123: initial letters different
:%#: and :%#: initial letters different
-
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.
|