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

Time:03-02

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

CodePudding user response:

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']
  • Related