python re.findall 包含括号的问题

>>> re.findall("a(b)?", "ab")
['b']
>>> re.findall("ab?", "ab")
['ab']
>>> re.findall('a(?:b)?', 'ab')
['ab']

(?:pattern)
匹配 pattern 但不获取匹配结果,也就是说这是一个非获取匹配,不进行存储供以后使用。这在使用 "或" 字符 (|) 来组合一个模式的各个部分是很有用。例如,'industr(?:y|ies) 就是一个比 'industry|industries' 更简略的表达式。

(?# …)就表示一个注释,里面的内容会被忽略。

 

>>> re.findall(r'\[My\](.+)\[/My\]', '[My]a[/My]\r\n[My]b[/My]') 
['a', 'b'] 
>>> re.findall(r'\[My\](.+)\[/My\]', '[My]a[/My]c[My]b[/My]') 
['a[/My]c[My]b'] 

 

>>>re.findall(r'\[My\](.+?)\[/My\]', '[My]a[/My]c[My]b[/My]')
['a', 'b']

 

http://www.cnblogs.com/huxi/archive/2010/07/04/1771073.html
http://www.yesky.com/imagesnew/software/vbscript/html/jsgrpRegExpSyntax.htm
http://bugs.python.org/issue8185

http://blog.csdn.net/smilelance/article/details/6529950

posted @ 2012-04-13 10:49  yarpee  阅读(1603)  评论(0编辑  收藏  举报