Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Regex pattern/function not retrieving the data from my datastring

I'm trying to pull certain parts out of a string using various regexs on continuously changing data strings.

For example the two regex's below should be ONLY pulling: Example $00.00 and bla $00.00

data = "Maybe Example $00.00 test $00.00 bla $00.00 dasdsadasd"
pull1 = re.match(r'^(Example) (\D\w+\D\w+)', data)
pull2 = re.match(r'^(bla) (\D\w+\D\w+)', data)

Anyone know what I'm doing wrong?

Thanks in advance - Hyflex

like image 786
Ryflex Avatar asked Jan 17 '26 21:01

Ryflex


1 Answers

If you want extract a partial string, use 'search' instead of 'match'. This code works fine:

data = "Maybe Example $00.00 test $00.00 bla $00.00 dasdsadasd"
pull1 = re.search(r'(Example) (\D\w+\D\w+)', data)
pull2 = re.search(r'(bla) (\D\w+\D\w+)', data)

print pull1.group(0)
print pull2.group(0)

I've removed '^' from your regex.

Good luck!

like image 53
lancif Avatar answered Jan 19 '26 20:01

lancif



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!