代码之家  ›  专栏  ›  技术社区  ›  Tristan King

特殊字符处的Python re.finditer错误

  •  1
  • Tristan King  · 技术社区  · 6 年前

    我的代码循环遍历一系列字符串,然后循环遍历另一个字符串中出现的每个字符串。直到它到达一个以问号(?)开头的字符串为止,这似乎一直有效?一) 。

    这是代码。

    dtID = 0
    for datum in sorted(datumList, key=operator.attrgetter('Sum'), reverse = True):
        datum.ID = dtID
        for foundDatum in re.finditer(datum.Name, text):
            datumLocList.append(DatumLoc(dtID,foundDatum.start()))
        dtID += 1
    

    我怎样才能解决这个问题?

    Traceback (most recent call last):
      File "C:\Users\trist\Documents\Python\The Compressor\The Compressor.py", line 97, in <module>
        compress()
      File "C:\Users\trist\Documents\Python\The Compressor\The Compressor.py", line 73, in compress
        for foundDatum in re.finditer(datum.Name, text):
      File "C:\Program Files\Python37\lib\re.py", line 230, in finditer
        return _compile(pattern, flags).finditer(string)
      File "C:\Program Files\Python37\lib\re.py", line 286, in _compile
        p = sre_compile.compile(pattern, flags)
      File "C:\Program Files\Python37\lib\sre_compile.py", line 764, in compile
        p = sre_parse.parse(p, flags)
      File "C:\Program Files\Python37\lib\sre_parse.py", line 930, in parse
        p = _parse_sub(source, pattern, flags & SRE_FLAG_VERBOSE, 0)
      File "C:\Program Files\Python37\lib\sre_parse.py", line 426, in _parse_sub
        not nested and not items))
      File "C:\Program Files\Python37\lib\sre_parse.py", line 651, in _parse
        source.tell() - here + len(this))
    re.error: nothing to repeat at position 0
    
    1 回复  |  直到 6 年前
        1
  •  3
  •   Robbie Dunn    6 年前

    要避免这种情况,可以使用 re.escape() 方法,该方法将转义模式字符串中的所有特殊字符。

    for foundDatum in re.finditer(re.escape(datum.Name), text):
        datumLocList.append(DatumLoc(dtID,foundDatum.start()))
    

    https://docs.python.org/3/library/re.html#re.escape