Python - 在多个定界符处拆分

Python -Split at multiple delimiters

>>> re.split("?|.", "How are you? I am talking to you. There?") 

错误:

>>> re.split("? |. ", "How are you? I am talking to you. There?")                
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\garg1\AppData\Local\Programs\Python\Python310\lib\re.py", line 230, in split
    return _compile(pattern, flags).split(string, maxsplit)
  File "C:\Users\garg1\AppData\Local\Programs\Python\Python310\lib\re.py", line 303, in _compile
    p = sre_compile.compile(pattern, flags)
  File "C:\Users\garg1\AppData\Local\Programs\Python\Python310\lib\sre_compile.py", line 764, in compile
    p = sre_parse.parse(p, flags)
  File "C:\Users\garg1\AppData\Local\Programs\Python\Python310\lib\sre_parse.py", line 950, in parse
    p = _parse_sub(source, state, flags & SRE_FLAG_VERBOSE, 0)
  File "C:\Users\garg1\AppData\Local\Programs\Python\Python310\lib\sre_parse.py", line 443, in _parse_sub
    itemsappend(_parse(source, state, verbose, nested + 1,
  File "C:\Users\garg1\AppData\Local\Programs\Python\Python310\lib\sre_parse.py", line 668, in _parse
    raise source.error("nothing to repeat",
re.error: nothing to repeat at position 0

您尝试拆分的字符在正则表达式中具有特殊含义。你会想要逃离他们。也许将它们添加到一个集合中而不是使用 |,此时不需要转义它们。我们还可以用 \s 指定结尾的 space 并用 +.

匹配一个或多个 space
>>> re.split(r'[?.]\s+', "How are you? I am talking to you")
['How are you', 'I am talking to you']