Tuesday, 15 January 2013

python - Overlapping regex -



python - Overlapping regex -

i want regex matches set of digits, 1 possible dot. if there dot , more digits after it, overlapping match previous digits, dot, , next digits. illustration string = 'aa323aa232.02.03.23.99aa87..0.111111.mm' desired results = [323, 232.02, 02.03, 03.23, 23.99, 87, 0.111111]

currently using:

import re = 'aa323aa232.02.03.23.99aa87..0.111111.mm' matches = re.findall(r'(?=(\d+\.{0,1}\d+))', i) print matches

output:

['323', '23', '232.02', '32.02', '2.02', '02.03', '2.03', '03.23', '3.23', '23.99', '3.99', '99', '87', '0.111111', '111111', '11111', '1111', '111', '11']

this uses lookahead assertion capturing, , look gobbling characters next rules:

>>> import re >>> = 'aa323aa232.02.03.23.99aa87..0.111111.mm' >>> re.findall(r'(?=(\d+(?:\.\d+)?))\d+(?:\.\d+(?!\.?\d))?', i)

output

['323', '232.02', '02.03', '03.23', '23.99', '87', '0.111111']

python regex

No comments:

Post a Comment