![]() |
VOOZH | about |
Pattern matching in Python allows you to search, extract, and validate text using regular expressions. Regex provides a flexible way to work with strings based on defined patterns.
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.
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.
Phone number found: 415-555-4242
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.
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.
415 area code: 415 number: 555-4242
If you would like to retrieve all the groups at once, use the groups(), method—note the plural form for the name.
</p><pre><code class="language-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())
</code></pre><p></p><h3 id="match-a-parenthesis" style="text-align:left"><b><strong>Match a parenthesis</strong></b></h3><p dir="ltr"><span>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. </span></p><p dir="ltr"><span>In this case, you need to escape the ( and ) characters with a backslash. Enter the following into the interactive shell: </span><gfg-tabs data-mode="light" data-run-ide="true"><gfg-tab slot="tab">Python
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))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.
Batman
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:
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.
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.
True
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 \?.
Batman Batwoman
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.
Batman
Example 2: In this example, we will match at least one occurrence of a pattern in the string.
Batwoman
Example 3: In this example, we will match more than one occurrence of a pattern in the string.
Batwowowowoman
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.
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.
True