Which Regular Expression flavour is used in Python?

人走茶凉 提交于 2019-12-23 16:15:12

问题


I want to which RegEx-Flavour is used for Python? Is it PCRE, Perl compatible or is it ICU or something else?


回答1:


It's compatible enough w/ Perl, meaning most Perl expressions will work unmodified. It aims to be Perl compatible but, of course, there are some minor differences.

It, technically, uses its own flavor of regular expressions. For instance, named groups were offered in Python regex long-before other implementations adopted the syntax. It also supports Unicode out of the box. Most Python extensions are supported elsewhere these days. See http://docs.python.org/library/re.html




回答2:


Unfortunately I cannot answer directly to the comment, but atomic blocks are an important feature (although few people understand their power), since you can create multibyte character sequences with it. I.e. in Windows a newline is \r\n.

Example: /(?>\r\n|\n|\r)\p{Any}/ matches to \n\r or \r., because that is a combination of a newline and any character literal, but it does not match to \r\n since nothing follows the newline.




回答3:


There is a good overview of Python's regex support here.

To sum it up:

The only significant features missing from Python's regex syntax are atomic grouping, possessive quantifiers and Unicode properties.

Atomic grouping and possessive quantifiers don't add much expressive power to the language. They are essentially for simpler and faster regexes.

Unicode is supported by the Python regex in that you can use literal unicode characters like Æ, but you can't use any Unicode escape codes in the regex itself.



来源:https://stackoverflow.com/questions/12022443/which-regular-expression-flavour-is-used-in-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!