in reply to Recognizing all-caps line

Try either of the following:

$line =~ m/^[[:upper:]]+$/    # or
$line =~ m/^\p{IsUpper}+$/

the [: ... :] is no character class, although it looks like one.
To make it (part of) a character class, you need another set of brackets around it.

don't go for   m/^[A-Z]+$/   for world peace international reasons ;-)
See   perldoc perlre   too.

Update: You might find the double negation more convenient:

$line !~ m/[[:^upper:]]/    # or
$line !~ m/\P{IsUpper}/

(think: do not match a line that contains anything that's not an uppercase letter)


Edit: corrected a number of mistakes.
Thanks especially to davido(++) for pointing out the ones I didn't find myself. Strange enough, while the necessity of the double brackets is not in doubt, I don't get any warnings when omitting them (perl5.8.3, Linux), the code just never tests true.
Another Edit: I guess that's because it looks like a character class to Perl. So why did it throw errors when davido tried it?
Another edit (and moved this discussion to the bottom of the node) -
I tried   m/[:^upper:]/;   which bypasses warnings.
Congratulations to us, we found a bug.

Cheers, Sören

Replies are listed 'Best First'.
Re^2: Recognizing all-caps line
by davido (Cardinal) on Oct 27, 2004 at 21:46 UTC

    Happy-the-monk has it right, that this is better done by posix character classes rather than enumerated-alphabet character classes. The problem with a [A-Z] character class is that it may exclude some perfectly legitimate upper-case characters in non-ASCII character encodings.

    The solution is POSIX character classes that do understand what constitutes an upper-case character in non-ASCII character encoding.

    There are a couple errors in Happy's response though. First, [:lower:] should be [:upper:] in order to match upper case characters, as the OP's question asked. Update: Looks like that first problem was fixed in an update.

    Second, the POSIX character class identifier has to be placed inside of a character class, leading to what might seem like a little bit of a wierd syntax, but is nevertheless necessary. Hense, the following:

    m/^[:upper:]+$/

    ... will fail to match, and will generate a warning if use warnings is active (Updated), whereas ...

    m/^[[:upper:]]$/

    will do what you want.


    Dave