python - Finding strings that are each separated by the same string -


i using python regular expressions. inputs strings these:

[in 1]: names = "john r spencer farrow r david k r gillian" [in 2]: names = "andrew r eli ltd" 

i.e. there number of parties (like "john", or "spencer farrow") separated " r ".

i want output list of strings, strings being parties. this

[out 1]: ["john", "spencer farrow", "david k", "gillian"] [out 2]: ["andrew", "eli ltd"] 

the code have tried variations of this

re.findall(r'[^(\sr\s)\w\s]+', names) 

i.e. try exclude specific string \sr\s (or " r ") character set including spaces , word characters.

please forgive ignorance, new regex.

something should work:

>>> import re >>> s = "john r spencer farrow r david k r gillian" >>> re.split(r'\br\b',s) ['john ', ' spencer farrow ', ' david k ', ' gillian'] 

this 1 rid of arbitrary amounts of whitespace too:

>>> re.split(r'\b(?:\s*)r(?:\s*)\b',s) ['john', 'spencer farrow', 'david k', 'gillian'] 

of course, if know seperator " r ", can use str.split:

>>> s.split(' r ') ['john', 'spencer farrow', 'david k', 'gillian'] 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -