Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python re - split a string before a character

how to split a string at positions before a character?

  • split a string before 'a'
  • input: "fffagggahhh"
  • output: ["fff", "aggg", "ahhh"]

the obvious way doesn't work:

>>> h=re.compile("(?=a)")

>>> h.split("fffagggahhh")

['fffagggahhh']

>>>
like image 878
kakarukeys Avatar asked Nov 04 '10 06:11

kakarukeys


1 Answers

Ok, not exactly the solution you want but I thought it will be a useful addition to problem here.

Solution without re

Without re:

>>> x = "fffagggahhh"
>>> k = x.split('a')
>>> j = [k[0]] + ['a'+l for l in k[1:]]
>>> j
['fff', 'aggg', 'ahhh']
>>> 
like image 182
pyfunc Avatar answered Oct 30 '22 15:10

pyfunc