简体   繁体   中英

Python -Split at multiple delimiters

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

Error:

>>> 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

The character you're trying to split on have special meaning in regular expressions. You'd want to escape them. And maybe add them to a set rather than using | , at which point escaping them is unnecessary. We can also specify the trailing space with \s and match one or more spaces with + .

>>> re.split(r'[?.]\s+', "How are you? I am talking to you")
['How are you', 'I am talking to you']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM