Friday, December 27, 2024
Google search engine
HomeLanguagesPattern matching in Python with Regex

Pattern matching in Python with Regex

You may be familiar with searching for text by pressing ctrl-F and typing in the words you’re looking for. Regular expressions go one step further: They allow you to specify a pattern of text to search for. In this article, we will see how pattern matching in Python works with Regex.

Regex in Python

Regular expressions, also called regex, are descriptions of a pattern of text. It can detect the presence or absence of a text by matching it with a particular pattern and also can split a pattern into one or more sub-patterns. For example, a \d in a regex stands for a digit character — that is, any single numeral between 0 and 9.

Check Phone Numbers Using Regex In Python

Following regex is used in Python to match a string of three numbers, a hyphen, three more numbers, another hyphen, and four numbers.

Any other string would not match the pattern.
\d\d\d-\d\d\d-\d\d\d\d

Regular expressions can be much more sophisticated. For example, adding a 3 in curly brackets ({3}) after a pattern is like saying, “Match this pattern three times.” So the slightly shorter regex is as follows:

\d{3}-\d{3}-\d{4}

It matches the correct phone number format.

Pattern Matching with Regular Expressions

A Regex object’s search() method searches the string it is passed for any matches to the regex. Match objects have a group() method that will return the actual matched text from the searched string. You can also see Regex cheetsheet for more information.

Example: Import the regex module with import re. Create a Regex object with the re.compile() function. (Remember to use a raw string.) Pass the string you want to search into the Regex object’s search() method. This returns a Match object. Call the Match object’s group() method to return a string of the actual matched text.

Python3




# Python program to illustrate
# Matching regex objects
import re
 
# regex object
phoneNumRegex = re.compile(r'\d\d\d-\d\d\d-\d\d\d\d')
mo = phoneNumRegex.search('My number is 415-555-4242.')
print('Phone number found: ' + mo.group())


Output:

Phone number found: 415-555-4242

Parentheses for Grouping and Capturing with Regex

One of the ways of pattern matching with Regex is by using Parentheses around the patterns. Let us see a few different examples for a better understanding.

Matching Objects

Say you want to separate the area code from the rest of the phone number. Adding parentheses will create groups in the regex: (\d\d\d)-(\d\d\d-\d\d\d\d). Then you can use the group() match object method to grab the matching text from just one group.

Python3




import re
phoneNumRegex = re.compile(r'(\d\d\d)-(\d\d\d-\d\d\d\d)')
mo = phoneNumRegex.search('My number is 415-555-4242.')
print(mo.group(1))
areaCode, number = mo.groups()
print("area code:", areaCode)
print("number:", number)


Output:

415
area code: 415
number: 555-4242

Retrieve all the Groups at Once

If you would like to retrieve all the groups at once, use the groups(), method—note the plural form for the name.

Python3




import re
phoneNumRegex = re.compile(r'(\d\d\d)-(\d\d\d-\d\d\d\d)')
mo = phoneNumRegex.search('My number is 415-555-4242.')
print(mo.groups())


Output:

('415', '555-4242')

Match a parenthesis

Parentheses have a special meaning in regular expressions, but what do you do if you need to match a parenthesis in your text. For instance, maybe the phone numbers you are trying to match have the area code set in parentheses. In this case, you need to escape the ( and ) characters with a backslash. Enter the following into the interactive shell:

Python3




import re
phoneNumRegex = re.compile(r'(\(\d\d\d\)) (\d\d\d-\d\d\d\d)')
mo = phoneNumRegex.search('My phone number is (415) 555-4242.')
print(mo.group(1))


Output:

(415)

The \( and \) escape characters in the raw string passed to re.compile() will match actual parenthesis characters.

Regular Expressions: Grouping and the Pipe Character

The | character is called a pipe. We can use it anywhere we want to match one of many expressions.

Example: The regular expression r’Batman|Tina Fey’ will match either ‘Batman’ or ‘Tina Fey’. When both Batman and Tina Fey occur in the searched string, the first occurrence of matching text will be returned as the Match object.

Python3




# Python program to illustrate
# Matching regex objects
# with multiple Groups with the Pipe
import re
heroRegex = re.compile (r'Batman|Tina Fey')
mo1 = heroRegex.search('Batman and Tina Fey.')
print(mo1.group())


Output:

'Batman'

Understanding Curly Braces in Regex

If we have a group that we want to repeat a specific number of times, follow the group in the regex with a number in curly brackets.

For example, the regex (Ha){3} will match the string ‘HaHaHa’, but it will not match ‘HaHa’, since the latter has only two repeats of the (Ha) group. Instead of just one number, you can specify a range in between the curly brackets. The regex (Ha){3, 5} will match ‘HaHaHa’, ‘HaHaHaHa’, and ‘HaHaHaHaHa’. You can also leave out the first or second number in the curly brackets to leave the minimum or maximum unbounded. (Ha){3, } will match three or more instances of the (Ha) group, while (Ha){, 5} will match zero to five instances. Curly brackets can help make your regular expressions shorter.

Example 1: In this example, we will use curly brackets to specify the occurrence of the pattern which we are looking for.

Python3




# Python program to illustrate
# Matching Specific Repetitions
# with Curly Brackets
import re
haRegex = re.compile(r'(Ha){3}')
mo1 = haRegex.search('HaHaHa')
print(mo1.group())


Output:

HaHaHa

Example 2: In this example, we will define the occurrence of the pattern using curly brackets and then search for if a specific pattern exists in it or not.

Python3




# Python program to illustrate
# Matching Specific Repetitions
# with Curly Brackets
import re
haRegex = re.compile(r'(Ha){3}')
mo2 = haRegex.search('Ha')== None
print(mo2)


Output:

True

Optional Operator or question mark (?) in Regular Expression

Sometimes there is a pattern that you want to match only optionally. That is, the regex should find a match whether or not that bit of text is there. The “?” character flags the group that precedes it as an optional part of the pattern.

Example 1: Here, we will search for a pattern with a pattern ‘Batman’ or ‘Batwoman’. The (wo)? part of the regular expression means that the pattern wo is an optional group. The regex will match text that has zero instances or one instance of wo in it. This is why the regex matches both ‘Batwoman’ and ‘Batman’. You can think of the ? as saying,groups “Match zero or one of the group preceding this question mark.”

If you need to match an actual question mark character, escape it with \?.

Python3




# Python program to illustrate
# optional matching
# with question mark(?)
import re
batRegex = re.compile(r'Bat(wo)?man')
mo1 = batRegex.search('The Adventures of Batman')
mo2 = batRegex.search('The Adventures of Batwoman')
print(mo1.group())
print(mo2.group())


Output:

Batman
Batwoman

Zero or More Pattern Matching with the Star

The * (called the star or asterisk) means “match zero or more” — the group that precedes the star can occur any number of times in the text. It can be completely absent or repeated over and over again. If you need to match an actual star character, prefix the star in the regular expression with a backslash, \*.

Example 1: In this example, we will match the zero occurrences of a pattern in the string. The (wo)* part of the regex matches zero instances of wo in the string.

Python3




# Python program to illustrate
# matching a regular expression
# with asterisk(*)
import re
batRegex = re.compile(r'Bat(wo)*man')
mo1 = batRegex.search('The Adventures of Batman')
print(mo1.group())


Output:

Batman

Example 2: In this example, we will match at least one occurrence of a pattern in the string.

Python




#python program to illustrate
#matching a regular expression
#with asterisk(*)
import re
batRegex = re.compile(r'Bat(wo)*man')
mo2 = batRegex.search('The Adventures of Batwoman')
print(mo2.group())


Output:

Batwoman

Example 3: In this example, we will match more than one occurrence of a pattern in the string.

Python




# Python program to illustrate
# matching a regular expression
# with asterisk(*)
import re
batRegex = re.compile(r'Bat(wo)*man')
mo3 = batRegex.search('The Adventures of Batwowowowoman')
print(mo3.group())


Output:

Batwowowowoman

One or More Pattern Matching with the Plus

While * means “match zero or more,” the + (or plus) means “match one or more.” Unlike the star, which does not require its group to appear in the matched string, the group preceding a plus must appear at least once. It is not optional. If you need to match an actual plus sign character, prefix the plus sign with a backslash to escape it: \+.

Example 1: In this example, we will match at least one occurrence of a pattern in the string.

Python3




# Python program to illustrate
# matching a regular expression
# with plus(+)
import re
batRegex = re.compile(r'Bat(wo)+man')
mo1 = batRegex.search('The Adventures of Batwoman')
print(mo1.group())


Output:

Batwoman

Example 2: In this example, the regex Bat(wo)+man will not match the string ‘The Adventures of Batman’ because at least one wo is required by the plus sign.

Python




# Python program to illustrate
# matching a regular expression
# with plus(+)
import re
batRegex = re.compile(r'Bat(wo)+man')
mo3 = batRegex.search('The Adventures of Batman')== None
print(mo3)


Output:

True

Related ArticleRegex Cheetsheet

RELATED ARTICLES

Most Popular

Recent Comments