Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl regular expression \U

Tags:

python

regex

perl

I am trying to convert some perl code to python. I came across this line

if($Var=~ /^\U/)
{ dosomething(); }

I have looked over the web for expression, but i am not able to comprehend its meaning.

\U uppercase till \E

\E end case modification

Please give an example of the correct usage if possible and how to approach for a python equivalent.

like image 594
pikapika Avatar asked Dec 04 '25 16:12

pikapika


1 Answers

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)
like image 123
ikegami Avatar answered Dec 07 '25 06:12

ikegami