-5

What would be the regex for separating a string into a list of items based on comas and/or white spaces?

Example: "item1, item2 item3 item4 , item5"

Result: ["item1", "item2", "item3", "item4", "item5"]

stefan.stt
  • 2,073
  • 4
  • 19
  • 37

3 Answers3

1

You can use the following regex [,\s]+ to find your delimiters.

Here is an example on python:

import re
text = "item1, item2 item3    item4 , item5"
result = re.split(r'[,\s]+', text)

This code return the following output:

['item1', 'item2', 'item3', 'item4', 'item5']
Antoine Dubuis
  • 3,837
  • 1
  • 12
  • 27
1

Not sure about what you want but I would do it in python with the following piece of code using a regex to split the string :

import re
s="item1, item2 item3    item4 , item5"
re.split('\s*,*\s*',s)

Gives as output :

['item1', 'item2', 'item3', 'item4', 'item5']
manu190466
  • 1,395
  • 1
  • 8
  • 14
0

Use this pattern:

item\d+

Regex Demo

Alireza
  • 2,011
  • 1
  • 5
  • 18