-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,357
  • 5
  • 24
  • 47

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
  • 4,974
  • 1
  • 15
  • 29
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,557
  • 1
  • 9
  • 17
0

Use this pattern:

item\d+

Regex Demo

Alireza
  • 2,103
  • 1
  • 6
  • 19