<分区>
我是第一次学习 re 模块,但遇到了一个错误。
代码-
import re
my_str='''pyhton
c++
java
c++
js node
ds algo
pyhton
js node
javac++
java
js node
ds algo'''
var = re.findall("c++",my_str)
它给出错误 - re.error: multiple repeat at position 2
<分区>
我是第一次学习 re 模块,但遇到了一个错误。
代码-
import re
my_str='''pyhton
c++
java
c++
js node
ds algo
pyhton
js node
javac++
java
js node
ds algo'''
var = re.findall("c++",my_str)
它给出错误 - re.error: multiple repeat at position 2
最佳答案
查看 Python RE module文档。 '+' 字符在 Regex 中具有特殊含义。表示前一个字符重复了一次或多次。
所以 'c++'
作为正则表达式实际上意味着“字符 'c' 重复了一次或多次,重复了一次或多次”
要真正识别字符 '+',您需要使用 '\'
将其转义。所以你的正则表达式变成了'c\+\+'
。
关于python - 为什么我收到错误 -"re.error: multiple repeat at position 2",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63614790/