You can use the re
module to work with regular expressions. Here’s an example that demonstrates the basic usage of regular expressions in Python:
import re
# Example string
text = "Hello, my phone number is 123-456-7890. Please call me!"
# Define a regular expression pattern
pattern = r'\d{3}-\d{3}-\d{4}'
# Use the pattern to find matches in the text
matches = re.findall(pattern, text)
# Print the matches
for match in matches:
print("Phone number:", match)
Code language: Python (python)
Output:
Phone number: 123-456-7890
Code language: Python (python)
In the example above, we import the re
module and define a regular expression pattern r'\d{3}-\d{3}-\d{4}'
, which matches the pattern of a phone number in the format ###-###-####
, where each #
represents a digit.
We then use the re.findall()
function to find all occurrences of the pattern in the given text
. It returns a list of all matching substrings.
Finally, we iterate over the matches
list and print each phone number that was found in the text.
Note that the r
prefix before the pattern string denotes a raw string in Python, which is used to treat backslashes (\
) as literal characters. This is important when working with regular expressions to avoid any unintended escape sequences.
How do you create a regular expression in Python?
To create a regular expression in Python, you need to use the re
module. Regular expressions are represented as strings and are enclosed between forward slashes (/
) or are defined as raw strings using the r
prefix.
Here are some common elements and patterns you can use to create regular expressions in Python:
- Literal Characters: Literal characters in a regular expression match themselves. For example, the regular expression
cat
matches the string “cat” exactly. - Character Classes: Character classes allow you to match any one character from a set of characters. Some common character classes include:
[abc]
: Matches any character that is either ‘a’, ‘b’, or ‘c’.[a-z]
: Matches any lowercase letter from ‘a’ to ‘z’.[0-9]
: Matches any digit from 0 to 9.[^abc]
: Matches any character except ‘a’, ‘b’, or ‘c’.
- Quantifiers: Quantifiers specify how many times a particular element should occur. Some common quantifiers include:
*
: Matches zero or more occurrences of the previous element.+
: Matches one or more occurrences of the previous element.?
: Matches zero or one occurrence of the previous element.{n}
: Matches exactly ‘n’ occurrences of the previous element.{n, m}
: Matches between ‘n’ and ‘m’ occurrences of the previous element.
- Anchors: Anchors are used to specify the position of a pattern in a string. Some common anchors include:
^
: Matches the start of a string.$
: Matches the end of a string.\b
: Matches a word boundary.
- Escape Sequences: Some characters have special meanings in regular expressions, such as
.
or*
. To match these characters literally, you need to escape them using a backslash (\
).
Here’s an example that combines some of these elements to create a regular expression pattern that matches a valid email address:
import re
pattern = r'^[\w\.-]+@[\w\.-]+\.\w+$'
email = "[email protected]"
if re.match(pattern, email):
print("Valid email address.")
else:
print("Invalid email address.")
Code language: Python (python)
Output:
Valid email address.
Code language: Python (python)
In the example above, the regular expression pattern r'^[\w\.-]+@[\w\.-]+\.\w+$'
matches a valid email address. Let’s break it down:
^
: Matches the start of the string.[\w\.-]+
: Matches one or more word characters, dots, or dashes (the username part of the email address).@
: Matches the ‘@’ symbol.[\w\.-]+
: Matches one or more word characters, dots, or dashes (the domain name part of the email address).\.
: Matches a dot.\w+
: Matches one or more word characters (the top-level domain part of the email address).$
: Matches the end of the string.
The re.match()
function is used to determine if the given email matches the pattern. If it does, the email is considered valid; otherwise, it is considered invalid.
Regular expressions can be complex and powerful, allowing you to match and manipulate strings with great flexibility. The examples above cover only the basics, but you can explore more advanced regular expression patterns and techniques depending on your specific needs.
Read More;
- What is TQDM in for loop Python?
- What is an example of a syntax error in Python?
- What is the dash function in Python?
- How to test a class with unittest Python?
- What is an example of a runtime error?
- How to run Python script from GitLab?
- What is an example of a string in Python?
- What is Break Statement in Python With Example
- What is Name Error Value Error in Python?
- What Is Variable Length Argument In Python With Example?
- What is NumPy in Python with example?
- Python Example For Arithmetic Operations On Lists