Home > Net >  REGEX - Find all values, after specific parameter and until new text started
REGEX - Find all values, after specific parameter and until new text started

Time:02-01

Sample text = "deductions amount XXX 123.45 555.55 YYY 22.00"

I need to find values after a specific parameter. For example if I will use XXX (\d [.,]\d ) I will find 123.45.

Question , what regex will need to be used to find ALL values after a specific parameter and NOT using something like this: XXX (\d [.,]\d ) (\d [.,]\d ) (\d [.,]\d ) and e.t.c.. So I need ALL value AFTER the specific parameter UP to the next text.

By using the sample text I would like to return:

  • XXX = ["123.45","555.5"]
  • YYY = ["22.00"]

CodePudding user response:

Something like:

import re

sample = 'deductions amount XXX 123.45 555.55 YYY 22.00'

for match in re.findall(r'([A-Z]{3}) ((?:\d*(?:\.\d )\s?) )', sample):
    print(f'{match[0]} = {match[1].split()}')

Result:

XXX = ['123.45', '555.55']
YYY = ['22.00']

CodePudding user response:

Use the enter image description here

  •  Tags:  
  • Related