Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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']
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading