๐ What are Regular Expressions (Regex)?

Regular Expressions (regex) are powerful tools used to search, match, and manipulate text patterns. Python provides the re
module to work with regex.
๐น 1. Importing the re
Module
import re
๐น 2. Basic Example
import re
text = "My phone number is 123-456-7890"
pattern = r"\d{3}-\d{3}-\d{4}"
match = re.search(pattern, text)
if match:
print("Found:", match.group())
โ Output:
Found: 123-456-7890
๐น 3. Useful Functions in re
Function | Description |
---|---|
re.search() | Finds the first match of a pattern |
re.findall() | Returns a list of all matches |
re.match() | Checks if the pattern matches at the start of the string |
re.sub() | Replaces matches with a new string |
re.split() | Splits a string based on the pattern |
re.compile() | Pre-compiles a regex pattern for reuse |
๐น 4. Common Regex Patterns
Pattern | Meaning | Example |
---|---|---|
. | Any character except newline | "a.c" โ matches "abc", "axc" |
\d | Digit (0โ9) | "123" |
\w | Word character (letters, digits, _) | "hello_123" |
\s | Whitespace (space, tab, newline) | "a b" |
^ | Start of string | "^Hello" |
$ | End of string | "world$" |
+ | One or more | \d+ โ "123", "4567" |
* | Zero or more | "ab*" โ "a", "abbb" |
? | Zero or one | "colou?r" โ "color", "colour" |
{n} | Exactly n times | \d{4} โ "2025" |
{n,m} | Between n and m times | \d{2,4} โ "20", "2025" |
[] | Character set | [aeiou] โ vowels |
` | ` | OR operator |
() | Grouping | "(abc)+" |
๐น 5. Examples
Example 1: Extract all email addresses
import re
text = "Contact us at support@example.com or sales@test.org"
emails = re.findall(r"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}", text)
print(emails)
โ Output:
['support@example.com', 'sales@test.org']
Example 2: Replace numbers with #
text = "User123 logged in at 10:30"
result = re.sub(r"\d", "#", text)
print(result)
โ Output:
User### logged in at ##:##
Example 3: Validate a phone number
pattern = r"^\d{3}-\d{3}-\d{4}$"
numbers = ["123-456-7890", "456-789", "987-654-3210"]
for num in numbers:
if re.match(pattern, num):
print(num, "is valid")
else:
print(num, "is invalid")
โ Output:
123-456-7890 is valid
456-789 is invalid
987-654-3210 is valid
๐น 6. Precompiled Regex for Efficiency
phone_pattern = re.compile(r"\d{3}-\d{3}-\d{4}")
print(phone_pattern.findall("Call 123-456-7890 or 987-654-3210"))
โ Output:
['123-456-7890', '987-654-3210']
๐ Summary
- Regex helps in pattern matching and text processing.
- Use the
re
module:search
,findall
,match
,sub
,split
. - Learn special characters like
\d
,\w
,+
,*
,{n,m}
,^
,$
. - Regex is widely used for validation, scraping, data cleaning, and automation.
Username = 08cHZGUU2X
Password = rZHWvz4aAO