I am trying to convert some perl code to python. I came across this line
if($Var=~ /^\U/)
{ dosomething(); }
The regex engine knows nothing of \U
. It's processed by the Perl parser in double quoted and regex literals.
"\Uabcdef" is the same as uc("abcdef")
"\Uabc\Edef" is the same as uc("abc")."def"
That means that
/^\Uabc/ is the same as /^ABC/
/^\U/ is the same as /^/
/^/
(and thus /^\U/
) is rather pointless, as it will always match.
If you wanted to make sure a string consists entirely of upper case letters, you'd use /^\p{Lu}*\z/
, or /^[A-Z]*\z/
if you have a very limited definition of letter.
Perl's
$var =~ /\Uabc\Edef/
would be written as follows in Python:
re.search("abc".upper() + "def", var)