# ALL ABOUT REGEX LIBRARY
The re module lets you use regular expressions (patterns used to search or match
text).
You can use it to:
find text
replace text
check if text follows a pattern (like email, phone number, etc.)
To use it:
import re
Basic Functions of re
| Function | Use
|
| -------------- |
----------------------------------------------------------------- |
| `[Link]()` | Search for the pattern **once** anywhere in the string
|
| `[Link]()` | Check if the pattern matches **from the beginning** of the
string |
| `[Link]()` | Find **all** matches in the string
|
| `[Link]()` | Replace parts of the string
|
Common Patterns
| Pattern | Meaning |
| ------- | ---------------------------------- |
| `.` | Any character except newline |
| `\d` | Digit (0-9) |
| `\D` | Not a digit |
| `\w` | Word character (a-z, A-Z, 0-9, \_) |
| `\W` | Not a word character |
| `\s` | Whitespace (space, tab, newline) |
| `\S` | Not a whitespace |
| `^` | Start of string |
| `$` | End of string |
| `[...]` | Any one of the characters inside |
| `*` | 0 or more times |
| `+` | 1 or more times |
| `?` | 0 or 1 time |
| `{n}` | Exactly n times |
✅ Easy Examples
1. Search for "cat" in a string
import re
text = 'I have a cat and a dog '
result = [Link]("cat", text)
print(result)
2 . Find all numbers in a string
text = 'My phone is 12345 and house number is 678'
numbers = [Link](r"\d+" , text)
print(numbers)
# ['12345', '678']
3 . Match if string starts with 'Hello'
text = 'Hello World'
if [Link](r"Hello", text):
print("Yes , it starts with Hello")
4. Replace all digits with #
text = "My number is 12345"
new_text = [Link](r"\d", "#" , text)
print(new_text)
Output : My number is #####
5. Find all words
text = "Hello World 123"
words = [Link](r\w+", text)
print(words)
Output : # ['Hello', 'World', '123']
Tip: Use r"..." for raw strings
Use r"" so Python doesn’t treat \ as an escape character.
# Correct
[Link](r"\d", "abc123")
# Wrong
[Link]("\\d", "abc123") # Works but hard to read