Python正则表达式:错误:没有什么可重复的

时间:2013-08-19 12:03:38

标签: python regex

我正在使用re.sub()从字符串中删除颜色控制代码(IRC),但每次都会失败。这就是我正在使用的:

re.sub('\x03(\d*)?,?(\d*)?', '', content)

错误:

  File "/usr/lib/python2.7/re.py", line 151, in sub
    return _compile(pattern, flags).sub(repl, string, count)
  File "/usr/lib/python2.7/re.py", line 242, in _compile
    raise error, v # invalid expression
error: nothing to repeat

我的正则表达式没有任何问题,所以有人可以解释我做错了吗?

1 个答案:

答案 0 :(得分:7)

你的正则表达式是合法的,但Python的正则表达式实现有一个错误,导致它在某些情况下拒绝嵌套的可选量词。

无论如何你不需要(...)? es:

re.sub('\x03\\d*,?\\d*', '', content)