I have some text input and I want to extract few information from the text. For that, I am trying to use Regular Expression and am able to do that except for two fields- rent and transfer.
The input text is as below-
my_str = "19 Aug standing order rent Apolo Housing Assoc. 500.00 50.00 20 Aug transfer from John wick saving a/c 200.00 130.90"
Now I want to extract rent like- rent 500.00 and transfer as transfer 200.00 but somehow only ‘rent’ and ‘transfer’ keywords are extracting only.
Below is my code in Python for the same-
import re find_rent = re.search(r"(rent)+([0-9,.]*)", my_str) found = find_rent.group() print(found)
With the above code, only ‘rent’ is extracted not ‘rent 500.00’. Similar code I am using for transfer also.
Please guide me on what I am doing wrong here.
Advertisement
Answer
You can use
b(transfer|rent)D+(d+(?:[,.]d+)*)
See the regex demo. Details:
b– a word boundary(transfer|rent)– Group 1: atransferorrentwordD+– one or more non-digits(d+(?:[,.]d+)*)– Group 2: one or more digits, and then zero or more occurrences of a comma/period and one or more digits
See the Python demo:
import re
s = '19 Aug standing order rent Apolo Housing Assoc. 500.00 50.00n20 Aug transfer from John wick saving a/c 200.00 130.90'
rx = r'b(transfer|rent)D+(d+(?:[,.]d+)*)'
for m in re.finditer(rx, s):
print(f'{m.group(1)} {m.group(2)}')
Output:
rent 500.00 transfer 200.00
For a single term search, you can use
import re
s = '19 Aug standing order rent Apolo Housing Assoc. 500.00 50.00n20 Aug transfer from John wick saving a/c 200.00 130.90'
w = 'rent'
rx = fr'b{w}D+(d+(?:[,.]d+)*)'
m = re.search(rx, s)
if m:
print(f'{w} {m.group(1)}')
See this Python demo.