Skip to content
Advertisement

How to find specific text with it’s correspondence value from a text input using Regex and Python?

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-

JavaScript

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-

JavaScript

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

JavaScript

See the regex demo. Details:

  • b – a word boundary
  • (transfer|rent) – Group 1: a transfer or rent word
  • D+ – 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:

JavaScript

Output:

JavaScript

For a single term search, you can use

JavaScript

See this Python demo.

Advertisement