Course Content
Python Indentation, Comments and Variables
0/2
Object Oriented Programming in Python
0/1
Exception Handling in Python
0/1
Sending emails with Python
0/1
Unit test in python programming
0/1
Python programming (zero to advance!!!)
About Lesson

Special Sequences

Special sequences are denoted by a backslash “ followed by a character, and they have special meanings for pattern matching.

The following are code examples demonstrating the use of each special sequence in regular expressions:

“`python
import re

# A: Returns a match if the specified characters are at the beginning of the string.
pattern = r’AThe’
test_string = “The quick brown fox jumps over the lazy dog.”

match = re.search(pattern, test_string)
print(“Match for A:”, match.group() if match else “No match”)

# b: Returns a match where the specified characters are at the beginning or at the end of a word.
pattern = r’bain’
test_string = “The rain in Spain falls mainly in the plain.”

matches = re.findall(pattern, test_string)
print(“Matches for bain:”, matches)

pattern = r’ainb’
matches = re.findall(pattern, test_string)
print(“Matches for ainb:”, matches)

# B: Returns a match where the specified characters are present, but NOT at the beginning (or at the end) of a word.
pattern = r’Bain’
test_string = “The rain in Spain falls mainly in the plain.”

matches = re.findall(pattern, test_string)
print(“Matches for Bain:”, matches)

pattern = r’ainB’
matches = re.findall(pattern, test_string)
print(“Matches for ainB:”, matches)

# d: Returns a match where the string contains digits (numbers from 0-9).
pattern = r’d’
test_string = “The price is $20.99.”

matches = re.findall(pattern, test_string)
print(“Matches for d:”, matches)

# D: Returns a match where the string DOES NOT contain digits.
pattern = r’D’
test_string = “The price is $20.99.”

matches = re.findall(pattern, test_string)
print(“Matches for D:”, matches)

# s: Returns a match where the string contains a whitespace character.
pattern = r’s’
test_string = “HellotWorldnHow are you?”

matches = re.findall(pattern, test_string)
print(“Matches for s:”, matches)

# S: Returns a match where the string DOES NOT contain a whitespace character.
pattern = r’S’
test_string = “HellotWorldnHow are you?”

matches = re.findall(pattern, test_string)
print(“Matches for S:”, matches)

# w: Returns a match where the string contains any word characters (characters from ‘a’ to ‘Z’, digits from 0-9, and the underscore _ character).
pattern = r’w’
test_string = “The quick brown fox jumps over the lazy dog 123.”

matches = re.findall(pattern, test_string)
print(“Matches for w:”, matches)

# W: Returns a match where the string DOES NOT contain any word characters.
pattern = r’W’
test_string = “The quick brown fox jumps over the lazy dog 123.”

matches = re.findall(pattern, test_string)
print(“Matches for W:”, matches)

# Z: Returns a match if the specified characters are at the end of the string.
pattern = r’dogZ’
test_string = “The quick brown fox jumps over the lazy dog.”

match = re.search(pattern, test_string)
print(“Match for Z:”, match.group() if match else “No match”)
“`

This code provides examples of each special sequence in action, demonstrating how they can be used to match specific patterns within text data using regular expressions.

Join the conversation