Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Some common regular expressions (regex) examples in Python
- Matching a single character:
- import re
- Match any digit (0-9)
- re.findall(r"\d", "abc123")
- Match any lowercase letter (a-z)
- re.findall(r"[a-z]", "abc123")
- Match any uppercase letter (A-Z)
- re.findall(r"[A-Z]", "ABC123")
- Match any letter (a-z, A-Z)
- re.findall(r"[a-zA-Z]", "ABC123")
- Matching a specific string:
- import re
- Match the string "cat"
- re.findall(r"cat", "The cat in the hat")
- Match the string "dog"
- re.findall(r"dog", "The cat in the hat")
- Matching a pattern of characters:
- import re
- Match any string that starts with "cat"
- re.findall(r"^cat", "cat in the hat")
- Match any string that ends with "dog"
- re.findall(r"dog$", "The cat and the dog")
- Match any string that contains "hat"
- re.findall(r"hat", "The cat in the hat")
- Matching repeated patterns:
- import re
- Match any string that has two consecutive digits
- re.findall(r"\d\d", "abc123")
- Match any string that has three consecutive lowercase letters
- re.findall(r"[a-z]{3}", "abc123")
- Match any string that has three or more consecutive digits
- re.findall(r"\d{3,}", "abc123")
- Match any string that has two or more consecutive lowercase letters
- re.findall(r"[a-z]{2,}", "abc123")
- Matching optional patterns:
- import re
- Match any string that has an optional digit at the end
- re.findall(r"\d?", "abc123")
- Match any string that has an optional lowercase letter at the end
- re.findall(r"[a-z]?", "abc123")
- Match any string that has an optional digit at the start
- re.findall(r"^\d?", "abc123")
- Match any string that has an optional lowercase letter at the start
- re.findall(r"^[a-z]?", "abc123")
- Matching groups of patterns:
- import re
- Match any string that has a word (consisting of letters) followed by a digit
- re.findall(r"\b\w+\b\d", "abc123")
- Match any string that has a digit followed by a word (consisting of letters)
- re.findall(r"\d\b\w+\b", "abc123")
- Here are a few more examples:
- Matching multiple patterns:
- import re
- Match any string that has a word (consisting of letters) followed by a digit, and then another word
- re.findall(r"\b\w+\b\d\b\w+\b", "abc123def")
- Matching multiple patterns:
- import re
- Match any string that has a digit, followed by a word (consisting of letters), followed by another digit
- re.findall(r"\d\b\w+\b\d", "abc123def")
- Match any string that has a word (consisting of letters) followed by a digit, or a digit followed by a word
- re.findall(r"\b\w+\b\d|\d\b\w+\b", "abc123def")
- Match any string that has a word (consisting of letters) followed by a digit, or a digit followed by a word, or both
- re.findall(r"\b\w+\b\d|\d\b\w+\b|\b\w+\b\d\b\w+\b", "abc123def")
- Matching multiple occurrences:
- import re
- Match any string that has three or more consecutive occurrences of the same letter
- re.findall(r"(\w)\1{2,}", "abc123def")
- Match any string that has three or more consecutive occurrences of the same digit
- re.findall(r"(\d)\1{2,}", "abc123def")
- Match any string that has two or more consecutive occurrences of the same letter, separated by a digit
- re.findall(r"(\w)\d(\1)", "abc123def")
- Match any string that has two or more consecutive occurrences of the same digit, separated by a letter
- re.findall(r"(\d)\w(\1)", "abc123def")
- Matching any character:
- import re
- Match any string that has a single character
- re.findall(r".", "abc123def")
- Match any string that has two consecutive characters
- re.findall(r"..", "abc123def")
- Match any string that has three consecutive characters
- re.findall(r"...", "abc123def")
- Match any string that has four consecutive characters
- re.findall(r"....", "abc123def")
- Matching any digit:
- import re
- Match any string that has a single digit
- re.findall(r"\d", "abc123def")
- Match any string that has two consecutive digits
- re.findall(r"\d\d", "abc123def")
- Match any string that has three consecutive digits
- re.findall(r"\d\d\d", "abc123def")
- Match any string that has four consecutive digits
- re.findall(r"\d\d\d\d", "abc123def")
- Matching any letter:
- import re
- Match any string that has a single letter
- re.findall(r"[a-zA-Z]", "abc123def")
- Match any string that has two consecutive letters
- re.findall(r"[a-zA-Z][a-zA-Z]", "abc123def")
- Match any string that has three consecutive letters
- re.findall(r"[a-zA-Z][a-zA-Z][a-zA-Z]", "abc123def")
- Matching any letter:
- import re
- Match any string that has four consecutive letters
- re.findall(r"[a-zA-Z][a-zA-Z][a-zA-Z][a-zA-Z]", "abc123def")
- Match any string that has two or more consecutive letters
- re.findall(r"[a-zA-Z]{2,}", "abc123def")
- Match any string that has three or more consecutive letters
- re.findall(r"[a-zA-Z]{3,}", "abc123def")
- Match any string that has four or more consecutive letters
- re.findall(r"[a-zA-Z]{4,}", "abc123def")
- Matching any word:
- import re
- Match any string that has a single word
- re.findall(r"\b\w+\b", "abc 123 def")
- Match any string that has two consecutive words
- re.findall(r"\b\w+\b\s\b\w+\b", "abc 123 def")
- Match any string that has three consecutive words
- re.findall(r"\b\w+\b\s\b\w+\b\s\b\w+\b", "abc 123 def")
- Match any string that has four consecutive words
- re.findall(r"\b\w+\b\s\b\w+\b\s\b\w+\b\s\b\w+\b", "abc 123 def")
- Match any string that has two or more consecutive words
- re.findall(r"\b\w+\b\s{1,}\b\w+\b", "abc 123 def")
- Match any string that has three or more consecutive words
- re.findall(r"\b\w+\b\s{1,}\b\w+\b\s{1,}\b\w+\b", "abc 123 def")
- Match any string that has four or more consecutive words
- re.findall(r"\b\w+\b\s{1,}\b\w+\b\s{1,}\b\w+\b\s{1,}\b\w+\b", "abc 123 def")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement