Home > OS >  How to match the string after the character and space in Python3?
How to match the string after the character and space in Python3?

Time:12-01

I have the following string:

'- Submission GMV / return finance027110/06 Abdul Rahman -26,00- Submission GMV / return finance02432548/08 Michael Scott -56,47- GMV success. 452630/10/21 Lehazq998890/92 60,00'

How can I return the values as:

[['- Submission PVT / return finance027110/06 Abdul Rahman','-26,00'],
 ['- Submission LTD / return finance02432548/08 Michael Scott','-56,47'],
 ['- GMV success. 452630/10/21 Lehazq998890/92', ' 60,00']]

I'm looping into multiple input. Hence, the count of output rows differs in each iteration. But the split pattern remains same.

CodePudding user response:

Your requirement is easy to come by using re.findall with two capture groups:

inp = "- Submission GMV / return finance027110/06 Abdul Rahman -26,00- Submission GMV / return finance02432548/08 Michael Scott -56,47- GMV success. 452630/10/21 Lehazq998890/92  60,00"
matches = re.findall(r'(-.*?) ([ -]\d (?:,\d )?)', inp)
print(matches)

This prints:

[('- Submission GMV / return finance027110/06 Abdul Rahman', '-26,00'),
 ('- Submission GMV / return finance02432548/08 Michael Scott', '-56,47'),
 ('- GMV success. 452630/10/21 Lehazq998890/92', ' 60,00')]

CodePudding user response:

try something like this data.split('- ')[1:]

  • Related