Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

breaking a string in python depending on character pattern

Tags:

python

regex

I have a string something like this:

a = '5:::{"test":[{"a":1,"b":2},{"a":2,"b":3}]}4:::{"something":[{"d":1,"e":2},{"d":2,"e":3}]}'

I would like to split this into a list with the values being:

['5:::{"test":[{"a":1,"b":2},{"a":2,"b":3}]}','4:::{"something":[{"d":1,"e":2},{"d":2,"e":3}]}']

I tried regular expressions like this:

b = re.findall(r'[0-9]:::.*(?=[0-9]:::)|(?=$)',a)

trying to match parts starting with a digit, followed by three colons, then any character until either a [0-9]::: is hit or the end of the string. This seems completely wrong but I'm at a loss on how to continue here.

Thx Markus

like image 304
user2348075 Avatar asked Mar 25 '23 02:03

user2348075


1 Answers

Use a lookahead assertion

re.findall(r'\d:::.+?(?=\d:::|$)', a)
like image 55
iruvar Avatar answered Mar 26 '23 16:03

iruvar