0% found this document useful (0 votes)
5 views2 pages

Regex Library: Functions & Examples

The document provides an overview of the Python 're' module for regular expressions, detailing its basic functions such as searching, matching, finding, and replacing text. It includes common patterns used in regex, along with easy examples demonstrating how to use these functions effectively. Additionally, it advises using raw strings to avoid escape character issues.

Uploaded by

Aman Kumar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

Regex Library: Functions & Examples

The document provides an overview of the Python 're' module for regular expressions, detailing its basic functions such as searching, matching, finding, and replacing text. It includes common patterns used in regex, along with easy examples demonstrating how to use these functions effectively. Additionally, it advises using raw strings to avoid escape character issues.

Uploaded by

Aman Kumar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

# 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

You might also like