-2

Hello guys I'm trying to split this string

"1, 2, 3 , 4 , 5, 6,7"

into this array

['1','2','3','4','5','6','7']

And get an error if there are characters or multiple spaces (one white space is acceptable following or before a comma)

I'm doing

re.split(r'\s?,\s?', some_string) 

but this does not return an error for invalid matches such as (",," or ", ,"). How could I accomplish that ?

Bruno Lopes
  • 199
  • 2
  • 7

1 Answers1

0

You don't necessarily need a regex for this

>>> s = "1, 2, 3 , 4 , 5, 6,7"
>>> [i.strip() for i in s.split(',')]
['1', '2', '3', '4', '5', '6', '7']
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218