Spex3
    

Regular Expressions (Regex) in Python 🔍🐍


    

    

Regular expressions (regex) help in searching, matching, and manipulating text based on patterns. Python’s re module provides powerful functions for working with regex.

1️⃣ Importing the re Module
Python provides the re module for regex operations.


import re

2️⃣ Basic Regex Functions in Python

Function Description
re.match() Checks if the pattern matches only at the beginning of the string.
re.search() Searches anywhere in the string for a match.
re.findall() Returns all matches as a list.
re.finditer() Returns an iterator of match objects.
re.sub() Replaces matches with a new string.
re.split() Splits a string based on the pattern.

3️⃣ Pattern Matching with match() and search()
✅ Using match() (Matches only at the beginning)


import re

text = "Hello, Python!"
pattern = r"Hello"

match = re.match(pattern, text)
print(match) #
✅ Using search() (Finds the pattern anywhere in the string)


text = "Say Hello to Python!"
pattern = r"Hello"

search = re.search(pattern, text)
print(search) #

4️⃣ Finding All Matches with findall()
✅ Example: Extracting All Digits from a String


text = "Order 3 apples and 5 oranges."
pattern = r"\d+" # Matches one or more digits

matches = re.findall(pattern, text)
print(matches) # ['3', '5']
✔ Returns a list of matches.

5️⃣ Using finditer() to Get Match Positions
✅ Example: Finding Positions of Matches


text = "The price is 100 dollars, not 200."
pattern = r"\d+"

matches = re.finditer(pattern, text)
for match in matches:
print(match.group(), "found at", match.span())
✔ .group() returns the matched text.
✔ .span() returns the start and end positions of the match.

6️⃣ Replacing Text with sub()

✅ Example: Replacing Numbers with XXX


text = "My number is 123-456-7890."
pattern = r"\d" # Match digits

new_text = re.sub(pattern, "X", text)
print(new_text) # My number is XXX-XXX-XXXX.
✔ sub(pattern, replacement, text) replaces all matches.

7️⃣ Splitting a String with split()

✅ Example: Splitting by Comma or Space


text = "apple, banana orange;grape"
pattern = r"[,; ]+" # Matches commas, semicolons, or spaces

result = re.split(pattern, text)
print(result) # ['apple', 'banana', 'orange', 'grape']
✔ split() can split by multiple delimiters.

8️⃣ Special Regex Characters & Patterns
Pattern Description Example Match
\d Digit (0-9) "123"
\D Non-digit "abc"
\w Word character (letters, digits, _) "hello_123"
\W Non-word character "@#!"
\s Whitespace (space, tab, newline) " "
\S Non-whitespace "hello"
. Any character except newline "a", "1", "@"
^ Start of string ^Hello matches "Hello world"
$ End of string world$ matches "Hello world"
* 0 or more repetitions ab* matches "a", "ab", "abb"
+ 1 or more repetitions ab+ matches "ab", "abb", but not "a"
? 0 or 1 repetition colou?r matches "color" and "colour"
{n} Exactly n repetitions \d{3} matches "123"
{n,} n or more repetitions \d{2,} matches "12", "123", "1234"
{n,m} Between n and m repetitions \d{2,4} matches "12", "123", "1234"
[] Character set [aeiou] matches any vowel
` ` OR operator
() Grouping (abc)+ matches "abc", "abcabc"

9️⃣ Summary

Function Use Case

match() Checks if a string starts with a pattern

search() Finds a pattern anywhere in a string

findall() Returns all matches in a list

finditer() Returns match objects with positions

sub() Replaces matched text

split() Splits a string using regex


    Date: 2025-03-24 00:00:00.000000