Loading...
Loading...

Regular Expressions in Python

Regular expressions, or regex, provide a way to search for patterns within strings. In Python, the re module is used for regex-based pattern matching, enabling advanced string manipulation.

1. Importing the `re` Module

The re module in Python offers functions for working with regular expressions. Import it with:

import re

Module Basics Quiz

Which module is used for regular expressions in Python?

  • regex
  • re
  • regexp

What does the 'r' prefix before a regex pattern mean?

  • It makes the pattern required
  • It creates a raw string (treats \ literally)
  • It makes the search case-sensitive

2. Basic Pattern Matching

Use re.search() to find the first match of a pattern within a string:

import re

pattern = r"hello"
text = "hello world"
match = re.search(pattern, text)
if match:
    print("Match found:", match.group())

The r before the pattern indicates a raw string, which treats backslashes as literal characters.

Pattern Matching Quiz

What does re.search() return if no match is found?

  • None
  • An empty string
  • False

How do you access the matched text from a match object?

  • match.text()
  • match.group()
  • match.string()

3. Using `re.findall()`

re.findall() returns all matches of a pattern in a list:

text = "cat bat rat mat"
matches = re.findall(r"\b\w+at\b", text)
print(matches)  # Output: ['cat', 'bat', 'rat', 'mat']

This example uses the word boundary \b to match words ending in "at".

findall() Quiz

What does re.findall() return if no matches are found?

  • An empty list
  • None
  • False

What does \b represent in regex?

  • A backspace character
  • A word boundary
  • The beginning of a string

4. Replacing Text with `re.sub()`

Use re.sub() to replace matches in a string:

text = "I like cats"
new_text = re.sub(r"cats", "dogs", text)
print(new_text)  # Output: I like dogs

sub() Quiz

How many replacements does re.sub() make by default?

  • Only the first match
  • All matches
  • None unless specified

How would you limit replacements to just the first match?

  • re.sub(pattern, repl, text, count=1)
  • re.sub_first(pattern, repl, text)
  • re.sub(pattern, repl, text, limit=1)

5. Pattern Modifiers

Modifiers control the behavior of regex. Commonly used flags include:

  • re.IGNORECASE or re.I: Case-insensitive matching.
  • re.MULTILINE or re.M: Multi-line matching for patterns like ^ and $.
text = "Hello world"
match = re.search(r"hello", text, re.IGNORECASE)
if match:
    print("Case-insensitive match found!")

Modifiers Quiz

Which flag makes matching case-insensitive?

  • re.IGNORECASE
  • re.CASELESS
  • re.NOCASE

What does re.MULTILINE affect?

  • Behavior of ^ and $ anchors
  • Matching across multiple strings
  • Pattern compilation speed

6. Common Regex Patterns

  • \d: Matches any digit (0-9).
  • \w: Matches any alphanumeric character (a-z, A-Z, 0-9, _).
  • \s: Matches any whitespace character (space, tab, newline).
  • ^: Matches the beginning of a string.
  • $: Matches the end of a string.

Example:

text = "My phone number is 123-456-7890"
pattern = r"\d{3}-\d{3}-\d{4}"
match = re.search(pattern, text)
if match:
    print("Phone number found:", match.group())

Patterns Quiz

What does \d match in regex?

  • Any digit (0-9)
  • Any decimal number
  • Any character

What does {3} in \d{3} mean?

  • Exactly 3 repetitions
  • At least 3 repetitions
  • Between 3 and 5 repetitions
0 Interaction
593 Views
Views
40 Likes
×
×
🍪 CookieConsent@Ptutorials:~

Welcome to Ptutorials

Note: We aim to make learning easier by sharing top-quality tutorials.

We kindly ask that you refrain from posting interactions unrelated to web development, such as political, sports, or other non-web-related content. Please be respectful and interact with other members in a friendly manner. By participating in discussions and providing valuable answers, you can earn points and level up your profile.

$ Allow cookies on this site ? (y/n)

top-home