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.

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.

Similar Reads

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....

Parentheses for Grouping and Capturing with Regex

...

Regular Expressions: Grouping and the Pipe Character

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....

Understanding Curly Braces in Regex

...

Optional Operator or question mark (?) in Regular Expression

...

Zero or More Pattern Matching with the Star

...

One or More Pattern Matching with the Plus

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