Explain the Python RegEx with Example
Explain the Python RegEx with Example
Ravi Vishwakarma is a dedicated Software Developer with a passion for crafting efficient and innovative solutions. With a keen eye for detail and years of experience, he excels in developing robust software systems that meet client needs. His expertise spans across multiple programming languages and technologies, making him a valuable asset in any software development project.
Mayank kumar Verma
14-Oct-2025Regex is a super smart search tool to finding patterns in text like email, phone number etc.
Basic stepup
pythone
import re
4 Main Functions You Need
1 Find if exists - re. search ()
Python
text = “My number is 9876543210”
2 Find ALL
python
re.findall(r'\d+', “I have 2 cats, 3 dogs”) # ['2', ‘3’,]
3 Replace
python
re.sub(r'\d', 'X', "Call 123") # Call XXX
Key Patterns:
\d = digit (0-9)
\w = letter
\s = space
+ = one or more
Example
python
emport re
text = "Email: john@site.com"
email = re.findall(r'\S+@\S+', text)
print(email) #[ ‘jhon@site.com’ ]
Done.
Anubhav Kumar
13-Oct-2025Python has a built-in module for RegEx:
Common RegEx Functions in Python (
remodule)re.match()re.search()re.findall()re.finditer()re.sub()re.split()Basic Syntax
.a.c^^Hello$world$*go*d+go+d?go?d{n}\d{3}{n,m}\d{2,4}[][aeiou]\d\d\w\w+\s\s()(ab)+Examples
1. Search for a Word
Output:
2. Check if String Starts with “Hello”
Output:
3. Find All Numbers
Output:
4. Replace Text
Output:
5. Split by Non-Alphabetic Characters
Output:
6. Validate Email Example
Output:
Match Object Details
If a match is found using
re.search()orre.match(), it returns a match object.Example:
Output:
Summary
re.search()re.search("word", text)re.findall()re.findall(r"\d+", text)re.sub()re.sub("old", "new", text)re.split()re.split(r"\s+", text)re.match()re.match(r"^[A-Za-z]+$", text)