re.compile(r'[[]]')匹配什么?

[英]What does re.compile(r'[[]]') match?


With Python's re module, why do the following act differently:

使用Python的re模块,为什么以下操作方式不同:

>>> r = re.compile(r'[][]')
>>> r.findall(r'[]')
['[', ']']
>>> r = re.compile(r'[[]]')
>>> r.findall(r'[]')
['[]']
>>> r.findall(r'][')
[]

3 个解决方案

#1


16  

The regular expression "[[]]" matches the substring "[]". The first [ in the expression begins a character class, and the first ] ends it. There is only one character ([) in the class, and then it has to be followed by the second ]. So the expression is "any of the characters in "[", followed by a "]".

正则表达式“[[]]”匹配子串“[]”。第一个[在表达式中开始一个字符类,第一个]结束它。类中只有一个字符([),然后必须跟着第二个字符。所以表达式是“[”中的任何字符,后跟一个“]”。

#2


4  

Character classes begin with a [ and end with the first ].

字符类以[以#结尾]开头。

So the expression [][] is a character class with the characters ] and [ as character classes must not be empty: [][]
And the expression [[]] is a character class with just [ and the single character ] after that: [[]]

所以表达式[] []是一个带有字符的字符类]和[因为字符类不能为空:[] []表达式[[]]是一个只有[和单个字符]的字符类:[[]]

#3


0  

and r'[][]' forms a character class {'[',']'}, and match either '[' or ']'.

并且r'[] []'形成一个字符类{'[',']'},并匹配'['或']'。

智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2009/03/07/3a797a27fb33bb1a3116a5bad1135df3.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告