Why am I getting the error-"re.error: multiple repeat at position 2"
Why am I getting the error-"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\+\+'
.
我总是建议使用在线正则表达式编辑器以交互方式 try-out 您的正则表达式。 regexr and regex101 就是此类编辑器的好例子。
我是第一次学习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\+\+'
.
我总是建议使用在线正则表达式编辑器以交互方式 try-out 您的正则表达式。 regexr and regex101 就是此类编辑器的好例子。