【发布时间】:2012-03-22 16:19:11
【问题描述】:
我想匹配一些前面有非数字或字符串开头的数字。
由于括号内的插入符号没有特殊含义,我不能使用那个,所以我检查了the reference,发现了另一种形式\A。
但是,当我尝试使用它时出现错误:
>>> s = '123'
>>> re.findall('[\D\A]\d+', s)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/re.py", line 177, in findall
return _compile(pattern, flags).findall(string)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/re.py", line 245, in _compile
raise error, v # invalid expression
sre_constants.error: internal: unsupported set operator
我做错了什么?
【问题讨论】:
-
您不能只在括号外使用插入符号,像这样吗?
^[A-Za-z]+?同样,括号内没有特殊含义的插入符号也不是完全正确的。如果插入符号是括号内的第一个字符,它将否定里面的字符集(说匹配除[^...]之外的所有字符 -
"一些数字前面有一个非数字或在字符串的开头" - 这不是说所有数字吗?只需使用
\d+... -
@lzkata:真正的用例更复杂。这只是一个简化。
-
我其实也有差不多的问题stackoverflow.com/questions/16257370/…>