regex examples

Sooner or later you will need to resort to using regular expressions if you are scraping text. This article will show you some useful Python (version 3) examples which you can use over and over again..

We’ll be using “re.search” and “group”

example 1 – parse a paragraph of text for the number of floors, so you need to find the word “floor” or equivalent, and then extract the number(s) directly before it.

re.search("(\d+)(\s[Ee]tage)", property_data)
We look for 1 or more digits followed by a space followed by “Etage “or “etage”

example 2 – parse a paragraph of text and extract the vendor details, so locate the text that immediately follows, but no more…

re.search(r'Vendor Name:([A-Z]).+?(?=Payment)', invoice)
re.search(r'Vendor Number:([0-9])+', invoice)

If you would like to copy the code from GitHub then visit the RGGH page here

Python Code

Previous article

Back Up MySQL