python - 为什么我收到错误 -"re.error: multiple repeat at position 2"

标签 python python-re

<分区>

我是第一次学习 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\+\+'

我总是建议使用在线正则表达式编辑器以交互方式试用您的正则表达式。 regexrregex101是此类编辑的好例子。

关于python - 为什么我收到错误 -"re.error: multiple repeat at position 2",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63614790/

相关文章:

python - if 函数使用 future 数组

Python 使用 re 在字符之间创建空格(方括号中的字符除外)

Python搜索字符模式,如果存在则缩进

python - 使用正则表达式提取特定单词之前的数字

Python-使用正则表达式模式替换 DataFrame 中列表中的单词

python - 如何处理 lambda 表达式中的正则表达式和空值?

python - 使用python层Caffe实现Bhattacharyya损失函数

python - Chrome Webdriver 无法完成产品结帐自动化

python - 初始化 QueryDict.fromkeys

python - Python 中的可中断线程连接