4

I have multiple strings where words are split with commas or periods:

string = ['apple,pear,grapes,carrot.cabbage,veggies.fruit,yard']

I would like to split this based on commas and periods:

string = ['apple','pear','grapes','carrot','cabbage','veggies','fruit','yard']

I only know how to use one condition for re.split:

re.split(',',string)

this won't split words that have periods in between. How can I split the whole string so that words are split when there are commas or periods in between?

Chiheb Nexus
  • 9,104
  • 4
  • 30
  • 43
halo09876
  • 2,725
  • 12
  • 51
  • 71

3 Answers3

8
>>> import re
>>> string = 'apple,pear,grapes,carrot.cabbage,veggies.fruit,yard'
>>> re.split(',|\.',string)
['apple', 'pear', 'grapes', 'carrot', 'cabbage', 'veggies', 'fruit', 'yard']

This splits on either , or . (which must be escaped as \.) using the alteration operator |.

It can also be written with a character class:

>>> re.split('[,.]',string)
['apple', 'pear', 'grapes', 'carrot', 'cabbage', 'veggies', 'fruit', 'yard']

But this is less general as neither character could be replaced with a phrase.

Dan D.
  • 73,243
  • 15
  • 104
  • 123
1
import re
string = 'apple,pear,grapes,carrot.cabbage,veggies.fruit,yard'
arr = re.split('[,.]', string)
print(arr)
user3429660
  • 2,420
  • 4
  • 25
  • 41
0

You can use chain.from_iterable from itertools module in order to handle your list, if you have many elements in your string variable

from itertools import chain

string = ['apple,pear,grapes,carrot.cabbage,veggies.fruit,yard']
final = list(chain.from_iterable(re.split(',', k) for k in string))
print(final)

Output:

['apple', 'pear', 'grapes', 'carrot.cabbage', 'veggies.fruit', 'yard']

And you can change only the pattern inside re.split() to split between ',' and '.':

from itertools import chain

string = ['apple,pear,grapes,carrot.cabbage,veggies.fruit,yard']
final = list(chain.from_iterable(re.split('[,.]', k) for k in string))
print(final)

Output:

['apple', 'pear', 'grapes', 'carrot', 'cabbage', 'veggies', 'fruit', 'yard']
Chiheb Nexus
  • 9,104
  • 4
  • 30
  • 43