Python RegEx

๐Ÿ” Python RegEx (Regular Expressions)

A Regular Expression (RegEx) is a sequence of characters used to match patterns in text โ€” such as emails, mobile numbers, dates, names, etc.

Python provides a built-in module for RegEx:

import re

โœ”๏ธ Common RegEx Methods in Python

Function Description
re.search() Searches for the first match
re.findall() Returns all matches
re.match() Checks if the pattern starts at the beginning of the string
re.split() Splits string by pattern
re.sub() Replaces matches

๐Ÿ“Œ 1. re.search() Example

import re

text = "Hello Python Developers!"
match = re.search("Python", text)

print(match)


๐Ÿ“Œ 2. re.findall() Example

import re

text = "Apple, Banana, Apple, Mango"
result = re.findall("Apple", text)
print(result) # ['Apple', 'Apple']


๐Ÿ“Œ 3. re.match() Example

import re

text = "Python is fun"
result = re.match("Python", text)

print(result)


๐Ÿ“Œ 4. re.split() Example

import re

text = "One,Two,Three,Four"
result = re.split(",", text)
print(result)


๐Ÿ“Œ 5. re.sub() Example (Replace Text)

import re

text = "Hello 123 World 456"
result = re.sub("\d", "*", text) # Replace digits
print(result)


๐Ÿง  RegEx Metacharacters

Pattern Meaning Example
. Any character except newline he..o โ†’ hello
^ Starts with ^Hello
$ Ends with world$
* 0 or more repetitions go* โ†’ g, go, gooo
+ 1 or more repetitions go+ โ†’ go, goo
? Optional colou?r โ†’ color, colour
{n} Exactly n occurrences \d{5}
{n,m} Between n and m repetitions \d{2,4}

๐Ÿ”ข Character Classes

Pattern Meaning
\d Matches digits (0-9)
\D Non-digit characters
\s Spaces
\S Non-spaces
\w Letters, numbers, underscore
\W Anything except letters/numbers/_

Example:

import re

text = "User123"
print(re.findall("\d", text)) # ['1', '2', '3']
print(re.findall("\w", text)) # ['U','s','e','r','1','2','3']


๐ŸŽฏ Using Groups ( )

import re

text = "Price: $150"
result = re.search("Price: \$(\d+)", text)
print(result.group(1))


๐Ÿ“ž Real-World Examples


โœ”๏ธ Validate Email

import re

email = "hello@gmail.com"
pattern = r"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$"

print(bool(re.match(pattern, email)))


โœ”๏ธ Validate Indian Phone Number

import re

number = "9876543210"
pattern = r"^[6-9]\d{9}$"

print(bool(re.match(pattern, number)))


โœ”๏ธ Extract All Numbers from Text

import re

text = "My phone is 98765 and zip is 395007"
print(re.findall("\d+", text))


โœ”๏ธ Check If String Only Contains Letters

import re

result = re.match("^[A-Za-z]+$", "Python")
print(bool(result))


๐Ÿ“Œ Summary Table

Task Function
Find first match re.search()
Find all matches re.findall()
Check beginning match re.match()
Replace text re.sub()
Split text re.split()

๐Ÿงช Practice Tasks

โœ” Find all email addresses in text
โœ” Validate password: must include letters, numbers, and symbols
โœ” Extract dates (DD/MM/YYYY)

CodeCapsule

Sanjit Sinha โ€” Web Developer | PHP โ€ข Laravel โ€ข CodeIgniter โ€ข MySQL โ€ข Bootstrap Founder, CodeCapsule โ€” Student projects & practical coding guides. Email: info@codecapsule.in โ€ข Website: CodeCapsule.in

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *