Python 正则表达式错误:在位置0没有要重复的内容
我正试图解析一个文本,其中我的正则表达式由于主题中提到的错误而失败 在下面的代码中,我只使用了那个部分,我不知道为什么它会失败!任何帮助都将不胜感激 虽然我在SO线程中看到了这个错误,但对我来说似乎有所不同Python 正则表达式错误:在位置0没有要重复的内容,python,regex,Python,Regex,我正试图解析一个文本,其中我的正则表达式由于主题中提到的错误而失败 在下面的代码中,我只使用了那个部分,我不知道为什么它会失败!任何帮助都将不胜感激 虽然我在SO线程中看到了这个错误,但对我来说似乎有所不同 import re t = '++cnt;' re.sub('++cnt','@'.join(c for c in '++cnt'),t) 错误回溯(最近一次调用上次) 在里面 1 t='++cnt;' 2. ---->3 re.sub('+cnt','@'.join(c代表'+cnt
import re
t = '++cnt;'
re.sub('++cnt','@'.join(c for c in '++cnt'),t)
错误回溯(最近一次调用上次)
在里面
1 t='++cnt;'
2.
---->3 re.sub('+cnt','@'.join(c代表'+cnt',t'中的c)
sub中的~/anaconda3/lib/python3.8/re.py(模式、repl、字符串、计数、标志)
208一个可调用的,它传递了Match对象并且必须返回
209要使用的替换字符串。”“”
-->210返回编译(模式、标志).sub(repl、字符串、计数)
211
212 def子网(模式、应答、字符串、计数=0、标志=0):
编译中的~/anaconda3/lib/python3.8/re.py(模式、标志)
302如果不是sre_compile.isstring(模式):
303 raise TypeError(“第一个参数必须是字符串或编译模式”)
-->304 p=sre_compile.compile(模式、标志)
305如果不是(标志和调试):
306如果len(\u cache)>=\u MAXCACHE:
编译中的~/anaconda3/lib/python3.8/sre_compile.py(p,标志)
762如果是字符串(p):
763模式=p
-->764 p=sre_parse.parse(p,标志)
765其他:
766模式=无
解析中的~/anaconda3/lib/python3.8/sre_parse.py(str、标志、状态)
946
947尝试:
-->948 p=_parse_sub(源、状态、标志和SRE_标志详细,0)
949除冗长外:
950#模式内的详细标志已打开。待
~/anaconda3/lib/python3.8/sre_parse.py in_parse_sub(源、状态、详细、嵌套)
441 start=source.tell()
442虽然正确:
-->443 itemsappend(_parse(源、状态、详细、嵌套+1、,
444非嵌套和非项目)
445如果不是sourcematch(“|”):
~/anaconda3/lib/python3.8/sre_parse.py in_parse(源、状态、详细、嵌套、第一)
666项=无
667如果不是项目或项目[0][0]位于:
-->668 raise source.错误(“无需重复”,
669 source.tell()-here+len(this))
670如果重复代码中的项目[0][0]:
错误:在位置0没有要重复的内容
在正则表达式+
中,表示前面的匹配组需要重复一次或多次。您将+
放在匹配模式的开头,这样就没有可以重复的内容。似乎您希望匹配字符+
。如果是这样,则需要转义+
,并且您的匹配模式为“\+\+cnt”应该是“\+\+cnt”+
在正则表达式中不是文字。它的意思是“前一个字符中的一个或多个”。如果您希望它是文字,则必须对其进行转义:re.sub(r“\+\+cnt)”,…
。是的,谢谢大家。我们还建议对任何正则表达式使用原始字符串r'.
。
error Traceback (most recent call last)
<ipython-input-482-2b724235a79b> in <module>
1 t = '++cnt;'
2
----> 3 re.sub('+cnt','@'.join(c for c in '++cnt'),t)
~/anaconda3/lib/python3.8/re.py in sub(pattern, repl, string, count, flags)
208 a callable, it's passed the Match object and must return
209 a replacement string to be used."""
--> 210 return _compile(pattern, flags).sub(repl, string, count)
211
212 def subn(pattern, repl, string, count=0, flags=0):
~/anaconda3/lib/python3.8/re.py in _compile(pattern, flags)
302 if not sre_compile.isstring(pattern):
303 raise TypeError("first argument must be string or compiled pattern")
--> 304 p = sre_compile.compile(pattern, flags)
305 if not (flags & DEBUG):
306 if len(_cache) >= _MAXCACHE:
~/anaconda3/lib/python3.8/sre_compile.py in compile(p, flags)
762 if isstring(p):
763 pattern = p
--> 764 p = sre_parse.parse(p, flags)
765 else:
766 pattern = None
~/anaconda3/lib/python3.8/sre_parse.py in parse(str, flags, state)
946
947 try:
--> 948 p = _parse_sub(source, state, flags & SRE_FLAG_VERBOSE, 0)
949 except Verbose:
950 # the VERBOSE flag was switched on inside the pattern. to be
~/anaconda3/lib/python3.8/sre_parse.py in _parse_sub(source, state, verbose, nested)
441 start = source.tell()
442 while True:
--> 443 itemsappend(_parse(source, state, verbose, nested + 1,
444 not nested and not items))
445 if not sourcematch("|"):
~/anaconda3/lib/python3.8/sre_parse.py in _parse(source, state, verbose, nested, first)
666 item = None
667 if not item or item[0][0] is AT:
--> 668 raise source.error("nothing to repeat",
669 source.tell() - here + len(this))
670 if item[0][0] in _REPEATCODES:
error: nothing to repeat at position 0