Day #95 - Regular Expressions in Python

Day #95 - Regular Expressions in Python

ยท

4 min read

Introduction

Welcome to my 95th blog post on the Python journey. On day 95, I learned about regular expressions that can be used to work with strings and text data in Python. Let's dive into more details and understand the basics of Regular expression in Python.

So let's get started......

Regular Expressions

  • They are called "regex" for short,

  • Regular expressions provide a powerful base to work with strings and text data.

  • We can match and manipulate strings data based on patterns, making it easy to perform complex string operations with just a few lines of code.

Below is the list of most commonly used Meta characters that are used to work with RE's . More such meta characters can be accessed from the below link:

Link :- Regular expressions

[]  Represent a character class
^   Matches the beginning
$   Matches the end
.   Matches any character except newline
?   Matches zero or one occurrence.
|   Means OR (Matches with any of the characters separated by it.
*   Any number of occurrences (including 0 occurrences)
+   One or more occurrences
{}  Indicate number of occurrences of a preceding RE to match.
()  Enclose a group of REs

're' Package

To use regular expressions in python we use 're' module. Below is the syntax for regular expression

import re

Searching for a pattern in 're' using re.search() Method

  • re.search() method either returns None (if the pattern doesnโ€™t match), or a re.MatchObject that contains information about the matching part of the string.

  • re.search() stops after the first match, hence this method is best used to test regular expressions rather than extracting data.

# Define a regular expression pattern
pattern = r"expression"

# Match the pattern against a string
text = "Hello, world!"

match = re.search(pattern, text)

if match:
    print("Match found!")
else:
    print("Match not found.")

Searching for a pattern in 're' using re.findall() Method

re.findall() function is used to find all occurrences of the pattern in a string

import re
pattern = r"expression"
text = "The cat is in the hat."

matches = re.findall(pattern, text)

print(matches)
# Output: ['cat', 'hat']

Replacing Pattern

import re
pattern = r"[a-z]+at"
text = "The cat is in the hat."

matches = re.findall(pattern, text)

print(matches)
# Output: ['cat', 'hat']

new_text = re.sub(pattern, "dog", text)

print(new_text)
# Output: "The dog is in the dog."

Extract Information from a string

import re

text = "The email address is example@example.com."

pattern = r"\w+@\w+\.\w+"

match = re.search(pattern, text)

if match:
    email = match.group()
    print(email)
# Output: example@example.com

Resources Used

You can watch the video of Day#95 by clicking on the below link ๐Ÿ‘‡๐Ÿ‘‡๐Ÿ‘‡๐Ÿ‘‡๐Ÿ‘‡

Conclusion

Thanks, guys for going through this blog post. On day 95, I covered the basics of regular expression and how they can be used to work by matching and manipulating strings and text data. I also learned about various other Regular expression functions such as searching a pattern, replacing a pattern and extracting information from the data

Thank you if you read this post and have found this post useful. I hope you have joined me and are enjoying my magical journey of Python coding. This is it for Day #95

See you in the next one.....


About Me

Hey Guys, I am Chintan Jain from CodeWithJain. I am a trader and content creator. I am also passionate about tech and hence wanted to explore the field of tech. I always wanted to learn to code so I watched many tutorials but procrastinated practicing coding. To get into the habit of coding consistently I am starting to BLOG with HASHNODE on daily basis.

I will document my coding journey from scratch and share my daily learnings in a blog post on HASHNODE. I hope you all will enjoy my content and my coding journey.

So what are you waiting for, smash the FOLLOW and LIKE buttons and follow along my coding journey, a step to create more beautiful digital products and empower people.


ย