In Python, regular expressions play a vital role in dealing with datasets. These metacharacters are critical in use in regular expressions. Here’re the top examples.
List of metacharacters
. ^ $ * + ? { } [ ] \ | ( )
The meaning of the preceding metacharacters is here:
?
(matches 0 or 1): the expressiona?
matches the stringa
(but notab
)*
(matches 0 or more): the expressiona*
matches the stringaaa
(but notbaa
)+
(matches 1 or more): the expression a+ matches aaa (but not baa)^
(beginning of line): the expression^[a]
matches the stringabc
(but notbc
)$
(end of line):[c]$
matches the stringabc
(but notcab
).
(a single dot): matches any character (except newline)
ON THIS PAGE
Python regular expression examples
You’ll find the real examples on python regular expression.
- Matching a specific pattern
- Matching any Char
- Matching one of more occurrence of Char
- Matching zero or more occurrence of Char
- Matching specific number of occurrences
#1 Matching a specific string pattern:
The search() methods searches for a pattern.
import re
pattern = "apple"
text = "I like apples and oranges"
match = re.search(pattern, text)
if match:
print("Match found!")
else:
print("Match not found!")
The output
Match found!
** Process exited - Return Code: 0 **
Press Enter to exit terminal
#2 Matching any character in a set:
The findall() method will find any matching character.
import re
pattern = "[aeiou]"
text = "Hello, how are you?"
matches = re.findall(pattern, text)
print(matches)
The output
['e', 'o', 'o', 'a', 'e', 'o', 'u']
** Process exited - Return Code: 0 **
Press Enter to exit terminal
Python CI/CD Pipeline how to deploy the code – A step-by-step guide to practice it on your laptop.
#3 Matching one or more occurrences of a character:
import re
pattern = "o+"
text = "Hello, how are you?"
matches = re.findall(pattern, text)
print(matches)
The output
['o', 'o', 'o']
** Process exited - Return Code: 0 **
Press Enter to exit terminal
#4 Matching zero or more occurrences of a character:
import re
pattern = "o*"
text = "Hello, how are you?"
matches = re.findall(pattern, text)
print(matches)
The output
['', '', '', '', 'o', '', '', '', 'o', '', '', '', '', '', '', '', 'o', '', '', '']
** Process exited - Return Code: 0 **
Press Enter to exit terminal
#5 Matching a specific number of occurrences of a character:
import re
pattern = "o{2}"
text = "Hello, how are you?"
matches = re.findall(pattern, text)
print(matches)
The output
[]
** Process exited - Return Code: 0 **
Press Enter to exit terminal
#6 Removing meta characters from a string
The sub() method removes meta characters from the string.
import re
text1 = "meta characters ? and / and + and ."
text2 = re.sub("[/\.*?=+]+","",text1)
print('text1:',text1)
print('text2:',text2)
The output
text1: meta characters ? and / and + and .
text2: meta characters and and and
** Process exited - Return Code: 0 **
Press Enter to exit terminal
Summary
- These are a few examples that you can do with regular expressions in Python.
- Regular expressions are a powerful tool for working with text, searching for specific patterns, validating input, etc.
References