πŸ’°πŸ’° Extracting price from invoices was never this easy: Regex Finale


Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python.

​

I am sure you are excited for the weekend. So am I. Today is our last stop in regex!

You now know how to fetch phones, emails, names from any text. There is one more layer that makes regex powerful.

With that you can -

  • catch both color, colour
  • match Nov, November irrespective of how they are written
  • Extract both $ and 200 from a $200 invoice, and more..

​

🎁 Bonus at the end: A great resource with 30 regex problems you can try

​

⭐ (*) for zero or more

'u' is optional β†’ for handling British vs American spellings

In:
import re
text = "I like the color blue. She likes the colour red."
print(re.findall(r"colou*r", text))
Out: ['color', 'colour']

​

❓ for zero or max one

Good to handle abbreviations

In:
text = "Nov 11, November 11"
# (?:ember)? β†’ "ember" is optional
print(re.findall(r"Nov(?:ember)?", text))
Out: ['Nov', 'November']

​

πŸ‘‰πŸ‘ˆ Control length (m,n)

If you are interested in error logs which are 2 or 3 digit

In:
text = "Error codes: 4, 23, 456, 7890"
# \d{2,3} β†’ match numbers 2–3 digits long
print(re.findall(r"\b\d{2,3}\b", text))
Out: ['23', '456']

​

🀝 Capture currency and amount separately with Groups ( )

In:
text = "Total: $250, Discount: €30"
# (\$|€) β†’ group 1: currency
# (\d+) β†’ group 2: number
print(re.findall(r"(\$|€)(\d+)", text))
Out: [('$', '250'), ('€', '30')]

​

↔️ Match either of them ( | )

Mention of cat or dog in reviews

In:
text = "The shelter has a cat and a dog."
print(re.findall(r"cat|dog", text))
Out: ['cat', 'dog']

​

πŸƒπŸ»β€β™‚οΈβ€βž‘οΈ Check if a is followed by b using (?=..)

Find if they have apple juice in the menu

In:
text = "apple pie, apple juice, apple tart"
# (?=\sjuice) β†’ match 'apple' only if followed by 'juice'
print(re.findall(r"apple(?=\sjuice)", text))
Out: ['apple']

​

🎯 Finally!! We are now done with regex!! 🎁 Do checkout this bonus resource - 30 regex exercises with solutions.


β­πŸ“£ That's it for today! If you liked it, please share it with anyone who will find it useful and share your feedback below 🐼

​

Pandas Daily

Beginner to Expert in Python in just 5 minutes

Read more from Pandas Daily

Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python. I am sure when you print C:\new_folder your code breaks due to \. Or you wonder how to print single (It's) or double ("Hello") quotes. Enter Escape Sequences, tiny backslash commands in Python that fix all of this. πŸ‘‰ Most common uses: Format using new lines Print tabs Single or double quotes in text Write file paths safely Print symbols like Ο€ or❀️ Let's Begin... πŸ‘‡ \n Print in next line - the one we all...

Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python. Think about the time you waste right-clicking and saving course lectures on YouTube one-by-one. Inefficient process, can cause errors and you lose focus. pytubefix eliminates that bottleneck. Today is day 3 of mastering YouTube with Python. You already know how to get stats of a single video; or download them in various formats. Now time to level up with playlists. Pytubefix has a Playlist object that...

Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python. Yesterday we introduced pytubefix - a powerful module to access YouTube. We downloaded a video as well. Cool, but basic. What if you want 1080p instead of 480p. Or prefer WebM over MP4. To answer all this - let's cover downloads in depth today. 🀫 I am 100% sure you can charm recruiters or your colleagues as they won't know this! πŸ“₯ Code Recap: How to download YouTube video In: # Import library from...