关于Pyhton正则报错: sre_constants.error: nothing to repeat at position
写了错误的正则表达式
1、\b 后带数量
将\d 误写成了\b , 尷尬!
>>> pattern = re.compile(r'123\b*hello')
输出:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "E:\Anacoda3\Lib\re.py", line 233, in compile
return _compile(pattern, flags)
File "E:\Anacoda3\Lib\re.py", line 301, in _compile
p = sre_compile.compile(pattern, flags)
File "E:\Anacoda3\Lib\sre_compile.py", line 562, in compile
p = sre_parse.parse(p, flags)
File "E:\Anacoda3\Lib\sre_parse.py", line 856, in parse
p = _parse_sub(source, pattern, flags & SRE_FLAG_VERBOSE, False)
File "E:\Anacoda3\Lib\sre_parse.py", line 415, in _parse_sub
itemsappend(_parse(source, state, verbose))
File "E:\Anacoda3\Lib\sre_parse.py", line 615, in _parse
source.tell() - here + len(this))
sre_constants.error: nothing to repeat at position 2
报错原因:由于 \b 为单词边界, * 的含义为出现任意次,也就是说, 单词只能有一个边界,不可能出现任意次,所以会报这个错