【问题标题】:Python 3 Regex IssuesPython 3 正则表达式问题
【发布时间】:2014-07-19 17:07:57
【问题描述】:

所以我在 Python 中匹配正则表达式字符串时遇到问题。我已经在http://regex101.com/ 上对其进行了测试,它运行良好。但是,当我尝试在我的代码中执行此操作时,它给了我一个格式错误的正则表达式错误

正则表达式为:“[^\\]\]PW\[”。我打算让它找到我的字符串]PW[,只要它不以反斜杠开头。 代码如下:

import sys,re
fileList = []
if len(sys.argv) == (0 or 1):
    fileList = ['tester.sgf']
else:
    fileList = str(sys.argv)
for sgfName in fileList:
    print(sgfName)
    currentSGF = open(sgfName,'r').read()
    currentSGF = currentSGF.replace("\r","") #clean the string
    currentSGF = currentSGF.replace("\n","")
for iterations in re.finditer("[^\\]\]PW\[",currentSGF): #here's the issue
    print(iterations.start(0), iterations.end(0), iterations.group())

我得到的错误是:

Traceback (most recent call last):
File "C:\Users\Josh\Desktop\New folder\sgflib1.0\test2.py", line 15, in <module>
for iterations in re.finditer("[^\\]\]PW\[",currentSGF):
File "C:\Python33\lib\re.py", line 210, in finditer
  return _compile(pattern, flags).finditer(string)
File "C:\Python33\lib\re.py", line 281, in _compile
  p = sre_compile.compile(pattern, flags)
File "C:\Python33\lib\sre_compile.py", line 491, in compile
  p = sre_parse.parse(p, flags)
File "C:\Python33\lib\sre_parse.py", line 747, in parse
  p = _parse_sub(source, pattern, 0)
File "C:\Python33\lib\sre_parse.py", line 359, in _parse_sub
  itemsappend(_parse(source, state))
File "C:\Python33\lib\sre_parse.py", line 485, in _parse
  raise error("unexpected end of regular expression")
sre_constants.error: unexpected end of regular expression

感谢您的帮助!

【问题讨论】:

    标签: python regex escaping character


    【解决方案1】:

    您需要使用 原始字符串文字 或将所有转义符加倍:

    re.finditer(r"[^\\]\]PW\[", currentSGF)
    

    re.finditer("[^\\\\]\\]PW\\[", currentSGF)
    

    否则,每个转义序列首先由 Python 解释为文字字符串值解释的一部分。 re.finditer 看到'[^\]]PW[ 的值,否则\]\[ 没有特殊含义。

    请参阅 Python 正则表达式 HOWTO 中的 The Backslash Plague

    【讨论】:

      猜你喜欢
      • 2010-10-20
      • 2014-11-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多