2 Python Regular Expressions Flashcards
(48 cards)
Regex Syntax Overview
. (dot) → Matches any character except a newline
^ → Matches the beginning of the string
$ → Matches the end of the string
\b
\d → Matches any digit (0-9)
\w → Matches any word character (letters, digits, and underscores)
\s → Matches any whitespace character (spaces, tabs, newlines)
- → Zero or more occurrences of the preceding element
+ → One or more occurrences of the preceding element
? → Zero or one occurrence of the preceding element
\ Signals a special sequence (can also be used to escape special characters)
| Either or
[ ] → Matches any one of the characters inside the brackets
{} Exactly the specified number of occurrences
() Capture and group
→ OR (alternation) operator
RegEx Functions
findall Returns a list containing all matches
search Returns a Match object if there is a
match anywhere in the string
split Returns a list where the string has
been split at each match
sub Replaces one or many matches with a
string
Match 3-letter words like cat, bat, rat
. (dot) in regex — matches any one character (except newline)
import re
text = “cat bat rat mat flat”
matches = re.findall(r”.at”, text)
print(matches)
Output:
[‘cat’, ‘bat’, ‘rat’, ‘mat’]
Find any 2-letter pattern end with a
text = “ba be bi bo bu ka ke ki ko ku”
matches = re.findall(r”.a”, text)
print(matches)
Output:
[‘ba’, ‘ka’]
Find phone numbers like 9X9 pattern
text = “Call me at 909 or 929 or 939”
matches = re.findall(r”9.9”, text)
print(matches)
Output:
[‘909’, ‘929’, ‘939’]
Match any character followed by ing (like sing, ring, etc.)
text = “sing ring ping king string”
matches = re.findall(r”.ing”, text)
print(matches)
Output:
[‘sing’, ‘ring’, ‘ping’, ‘king’]
Extract all numbers from a sentence.
import re
text = “The address is 1234 Main Street, 5th floor.”
numbers = re.findall(r”\d+”, text)
print(numbers)
Output:
[‘1234’, ‘5’]
^ (Caret)
^ pattern checks if that pattern is right at the start.
Use re.match() or re.findall(…, re.MULTILINE) depending on single vs multi-line text.
How to check if a sentence starts with “Hello”?
import re
text = “Hello world, NLP is powerful.”
result = re.match(r”^Hello”, text)
print(bool(result)) # True
From a multi-line string, get lines that start with a digit.
import re
text = “"”123 data points collected
Text starts here
56 more samples”””
matches = re.findall(r”^(\d+.*)”, text, re.MULTILINE)
print(matches)
Output:
[‘123 data points collected’, ‘56 more samples’]
Get lines where the first character is uppercase.
import re
text = “"”apple is healthy
Banana is yellow
grapes are green
Mango is sweet”””
matches = re.findall(r”^[A-Z].*”, text, re.MULTILINE)
print(matches)
Output:
[‘Banana is yellow’, ‘Mango is sweet’]
Validate if a sentence begins with a specific phrase
import re
sentence = “The model achieved high accuracy.”
if re.match(r”^The model”, sentence):
print(“✅ Starts with ‘The model’”)
else:
print(“❌ Doesn’t start with ‘The model’”)
Filter words starting with a specific letter (in list)
import re
words = [“Apple”, “Banana”, “Avocado”, “Mango”, “Apricot”]
for word in words:
if re.match(r”^A”, word):
print(word)
Output:
Apple
Avocado
Apricot
$
$ – Matches only if the pattern appears at the end of the string.
Does the sentence end with “India”?
import re
text = “I love my country India”
match = re.search(r”India$”, text)
print(bool(match))
Output:
True
Check if a file ends with “.txt”
text = “notes_backup_2025.txt”
match = re.search(r”.txt$”, text)
print(bool(match))
Output:
True
Check if a sentence ends with a number
text = “Token count is 99”
match = re.search(r”\d+$”, text)
print(bool(match))
Output:
True
Check if a line ends with punctuation (like .)
text = “This is a full sentence.”
match = re.search(r”.$”, text)
print(bool(match))
Output:
True
Filter strings that end with a specific word (like ‘done’)
text = “Task completed and marked done”
match = re.search(r”done$”, text)
print(bool(match))
Output:
True
How can I find all vowels in a sentence?
import re
text = “NLP is fun and powerful!”
vowels = re.findall(r”[aeiouAEIOU]”, text)
print(vowels)
Output:
[‘I’, ‘u’, ‘a’, ‘o’, ‘e’, ‘u’]
How do I find all punctuation marks like ., !, or ,?
import re
text = “Hello, world! NLP is amazing.”
punct = re.findall(r”[.,!]”, text)
print(punct)
Output:
[’,’, ‘!’, ‘.’]
How can I find all characters that are NOT vowels?
Use [^aeiouAEIOU] — the ^ inside brackets means NOT.
import re
text = “ChatGPT”
non_vowels = re.findall(r”[^aeiouAEIOU]”, text)
print(non_vowels)
Output:
[‘C’, ‘h’, ‘t’, ‘G’, ‘P’, ‘T’]
How do I match only digits from 1 to 5?
Use [1-5] to match any single digit from 1 through 5.
import re
text = “My scores: 1, 2, 5, 7, 9”
result = re.findall(r”[1-5]”, text)
print(result)
Output:
[‘1’, ‘2’, ‘5’]
How do I match all lowercase letters?
Use [a-z] to match any lowercase letter.
import re
text = “Deep Learning!”
lowers = re.findall(r”[a-z]”, text)
print(lowers)
Output:
[‘e’, ‘e’, ‘p’, ‘e’, ‘a’, ‘r’, ‘n’, ‘i’, ‘n’, ‘g’]