Python Regex Split String While Keeping Delimiter With Value
I'm trying to parse a text file with name:value elements in it into lists with 'name:value'... Here's a twist: The values will sometimes be multiple words or even multiple lines
Solution 1:
Use a look-ahead assertion:
>>> re.split(r'\s(?=\w+:)', post)
['price:44.55',
'name:John Doe',
'title:Super Widget',
'description:This widget slices, dices, and drives your kids to soccer practice\r\nIt even comes with Super Widget Mini!']
Of course, it would still fail if there are some words followed immediately by a colon in your values.
Post a Comment for "Python Regex Split String While Keeping Delimiter With Value"