vendredi 3 avril 2015

Python split string based on conditional

I want to split strings using a comma delimiter if the comma is preceded by a certain regex. Consider the case where my strings are in the format: "(bunch of stuff that might have commas) FOO_REGEX, (other stuff that might have commas) FOO_REGEX, ..." and I want to split the string on commas, but only if they're preceded by FOO_REGEX: ["(bunch of stuff that might have commas) FOO_REGEX", "(other stuff that might have commas) FOO_REGEX", tc.].


As a concrete example, consider splitting the following string:



"hi, hello! $$asdf, I am foo, bar $$jkl, cool"


into this list of three strings:



["hi, hello! $$asdf",
"I am foo, bar $$jkl",
"cool"]


Is there any easy way to do this in python?


Aucun commentaire:

Enregistrer un commentaire