Search code examples
pythonpython-3.xregexpython-re

Split text only by comma or (comma and space) not only with space


I want a regex expression that extract values seperated by commas.

Sample input: "John Doe, Jack, , Henry,Harry,,Rob" Expected output:

John Doe
Jack
Henry
Harry
Rob

I tried [\w ]+ but blank values and extra spaces are getting included


Solution

  • Given:

    >>> s="John Doe, Jack, , Henry,Harry,,Rob"
    

    You can do:

    >>> [e for e in re.split(r'\s*,\s*',s) if e]
    ['John Doe', 'Jack', 'Henry', 'Harry', 'Rob']