简体   繁体   English

Perl正则表达式\ U.

[英]Perl regular expression \U

I am trying to convert some perl code to python. 我试图将一些perl代码转换为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 \\ U大写直到\\ E.

\\E end case modification \\ E结束案例修改

Please give an example of the correct usage if possible and how to approach for a python equivalent. 如果可能的话,请举例说明正确的用法以及如何处理python等价物。

The regex engine knows nothing of \\U . 正则表达式引擎一无所知\\U It's processed by the Perl parser in double quoted and regex literals. 它由双引号和正则表达式文字中的Perl解析器处理。

"\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. /^/ (因而/^\\U/ )是毫无意义的,因为它总是匹配。

If you wanted to make sure a string consists entirely of upper case letters, you'd use /^\\p{Lu}*\\z/ , or /^[AZ]*\\z/ if you have a very limited definition of letter. 如果你想确保一个字符串完全由大写字母组成,你可以使用/^\\p{Lu}*\\z/ ,或/^[AZ]*\\z/如果你的字母定义非常有限。


Perl's Perl的

$var =~ /\Uabc\Edef/

would be written as follows in Python: 将在Python中编写如下:

re.search("abc".upper() + "def", var)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM