为什么出现此错误:sre_constants.error:在位置2没有重复的内容?

时间:2019-04-20 05:51:32

标签: regex python-3.x

这是尝试匹配模式时发生的错误

模式= re.compile(r'\ b * \ d {3} \ d {3}-\ d {4} \ b *') def match_exactly(电话):     电话= pattern.fullmatch(电话)     如果是电话:         返回phone.group()     不返回

打印(精确匹配)

1 个答案:

答案 0 :(得分:0)

\b(单词边界)实际上表示以下4个断言之一:

+----------------+-------------------+------------------+
|                |    Before the     |    After the     |
|                | current position  | current position |
+----------------+-------------------+------------------+
| (?<=^)(?=\w)   | Start of string   | Word char        |
| (?<=\w)(?=$)   | Word char         | End of string    |
| (?<=\W)(?=\w)  | Non-word char     | Word char        |
| (?<=\w)(?=\W)  | Word char         | Non-word char    |
+----------------+-------------------+------------------+

因为断言(至少在上述情况下)是关于 当前位置之前和/或之后的内容,无法重复

我尝试执行您的代码,该错误发生在re.compile指令上。

所以您应该将其更改为:

pattern = re.compile(r'\b\d{3} \d{3}-\d{4}\b')

({{1}之后没有*)。