If you are interested to learn about the Python Inheritance
A regular expression is a special sequence of characters that helps you match or find other strings or sets of strings, using a specialized syntax held in a pattern. Regular expressions are widely used in UNIX world. The Python module re provides full support for Perl-like regular expressions in Python. The re module raises the exception re.error if an error occurs while compiling or using a regular expression. We would cover two important functions, which would be used to handle regular expressions. But a small thing first: There are various characters, which would have special meaning when they are used in regular expression. To avoid any confusion while dealing with regular expressions, we would use Raw Strings as r’expression’.
The match Function
This function attempts to match RE pattern to string with optional flags.
Here is the syntax for this function −
re.match(pattern, string, flags=0)
Here is the description of the parameters −
Sr.No. | Parameter & Description |
---|---|
1 | patternThis is the regular expression to be matched. |
2 | stringThis is the string, which would be searched to match the pattern at the beginning of string. |
3 | flagsYou can specify different flags using bitwise OR (|). These are modifiers, which are listed in the table below. |
The re.match function returns a match object on success, None on failure. We usegroup(num) or groups() function of match object to get matched expression.
Sr.No. | Match Object Method & Description |
---|---|
1 | group(num=0)This method returns entire match (or specific subgroup num) |
2 | groups()This method returns all matching subgroups in a tuple (empty if there weren’t any) |
Example
#!/usr/bin/python import re line = "Cats are smarter than dogs" matchObj = re.match( r'(.*) are (.*?) .*', line, re.M|re.I) if matchObj: print "matchObj.group() : ", matchObj.group() print "matchObj.group(1) : ", matchObj.group(1) print "matchObj.group(2) : ", matchObj.group(2) else: print "No match!!"
When the above code is executed, it produces following result −
matchObj.group() : Cats are smarter than dogs
matchObj.group(1) : Cats
matchObj.group(2) : smarter
The search Function
This function searches for first occurrence of RE pattern within string with optional flags.
Here is the syntax for this function −
re.search(pattern, string, flags=0)
Here is the description of the parameters −
Sr.No. | Parameter & Description |
---|---|
1 | patternThis is the regular expression to be matched. |
2 | stringThis is the string, which would be searched to match the pattern anywhere in the string. |
3 | flagsYou can specify different flags using bitwise OR (|). These are modifiers, which are listed in the table below. |
The re.search function returns a match object on success, none on failure. We use group(num) or groups() function of match object to get matched expression.
Sr.No. | Match Object Methods & Description |
---|---|
1 | group(num=0)This method returns entire match (or specific subgroup num) |
2 | groups()This method returns all matching subgroups in a tuple (empty if there weren’t any) |
Example
#!/usr/bin/python import re line = "Cats are smarter than dogs"; searchObj = re.search( r'(.*) are (.*?) .*', line, re.M|re.I) if searchObj: print "searchObj.group() : ", searchObj.group() print "searchObj.group(1) : ", searchObj.group(1) print "searchObj.group(2) : ", searchObj.group(2) else: print "Nothing found!!"
When the above code is executed, it produces following result −
searchObj.group() : Cats are smarter than dogs
searchObj.group(1) : Cats
searchObj.group(2) : smarter
Matching Versus Searching
Python offers two different primitive operations based on regular expressions: match checks for a match only at the beginning of the string, while search checks for a match anywhere in the string (this is what Perl does by default).
Example
#!/usr/bin/python import re line = "Cats are smarter than dogs"; matchObj = re.match( r'dogs', line, re.M|re.I) if matchObj: print "match --> matchObj.group() : ", matchObj.group() else: print "No match!!" searchObj = re.search( r'dogs', line, re.M|re.I) if searchObj: print "search --> searchObj.group() : ", searchObj.group() else: print "Nothing found!!"
When the above code is executed, it produces the following result −
No match!!
search --> searchObj.group() : dogs
Search and Replace
One of the most important re methods that use regular expressions is sub.
Syntax
re.sub(pattern, repl, string, max=0)
This method replaces all occurrences of the RE pattern in string with repl, substituting all occurrences unless max provided. This method returns modified string.
Example
#!/usr/bin/python import re phone = "2004-959-559 # This is Phone Number" # Delete Python-style comments num = re.sub(r'#.*$', "", phone) print "Phone Num : ", num # Remove anything other than digits num = re.sub(r'\D', "", phone) print "Phone Num : ", num
When the above code is executed, it produces the following result −
Phone Num : 2004-959-559
Phone Num : 2004959559
Regular Expression Modifiers: Option Flags
Regular expression literals may include an optional modifier to control various aspects of matching. The modifiers are specified as an optional flag. You can provide multiple modifiers using exclusive OR (|), as shown previously and may be represented by one of these −
Sr.No. | Modifier & Description |
---|---|
1 | re.IPerforms case-insensitive matching. |
2 | re.LInterprets words according to the current locale. This interpretation affects the alphabetic group (\w and \W), as well as word boundary behavior(\b and \B). |
3 | re.MMakes $ match the end of a line (not just the end of the string) and makes ^ match the start of any line (not just the start of the string). |
4 | re.SMakes a period (dot) match any character, including a newline. |
5 | re.UInterprets letters according to the Unicode character set. This flag affects the behavior of \w, \W, \b, \B. |
6 | re.XPermits “cuter” regular expression syntax. It ignores whitespace (except inside a set [] or when escaped by a backslash) and treats unescaped # as a comment marker. |
Regular Expression Patterns
Except for control characters, (+ ? . * ^ $ ( ) [ ] { } | \), all characters match themselves. You can escape a control character by preceding it with a backslash. Following table lists the regular expression syntax that is available in Python −
Sr.No. | Pattern & Description |
---|---|
1 | ^Matches beginning of line. |
2 | $Matches end of line. |
3 | .Matches any single character except newline. Using m option allows it to match newline as well. |
4 | […]Matches any single character in brackets. |
5 | [^…]Matches any single character not in brackets |
6 | re*Matches 0 or more occurrences of preceding expression. |
7 | re+Matches 1 or more occurrence of preceding expression. |
8 | re?Matches 0 or 1 occurrence of preceding expression. |
9 | re{ n}Matches exactly n number of occurrences of preceding expression. |
10 | re{ n,}Matches n or more occurrences of preceding expression. |
11 | re{ n, m}Matches at least n and at most m occurrences of preceding expression. |
12 | a| bMatches either a or b. |
13 | (re)Groups regular expressions and remembers matched text. |
14 | (?imx)Temporarily toggles on i, m, or x options within a regular expression. If in parentheses, only that area is affected. |
15 | (?-imx)Temporarily toggles off i, m, or x options within a regular expression. If in parentheses, only that area is affected. |
16 | (?: re)Groups regular expressions without remembering matched text. |
17 | (?imx: re)Temporarily toggles on i, m, or x options within parentheses. |
18 | (?-imx: re)Temporarily toggles off i, m, or x options within parentheses. |
19 | (?#…)Comment. |
20 | (?= re)Specifies position using a pattern. Doesn’t have a range. |
21 | (?! re)Specifies position using pattern negation. Doesn’t have a range. |
22 | (?> re)Matches independent pattern without backtracking. |
23 | \wMatches word characters. |
24 | \WMatches nonword characters. |
25 | \sMatches whitespace. Equivalent to [\t\n\r\f]. |
26 | \SMatches nonwhitespace. |
27 | \dMatches digits. Equivalent to [0-9]. |
28 | \DMatches nondigits. |
29 | \AMatches beginning of string. |
30 | \ZMatches end of string. If a newline exists, it matches just before newline. |
31 | \zMatches end of string. |
32 | \GMatches point where last match finished. |
33 | \bMatches word boundaries when outside brackets. Matches backspace (0x08) when inside brackets. |
34 | \BMatches nonword boundaries. |
35 | \n, \t, etc.Matches newlines, carriage returns, tabs, etc. |
36 | \1…\9Matches nth grouped subexpression. |
37 | \10Matches nth grouped subexpression if it matched already. Otherwise refers to the octal representation of a character code. |
Regular Expression Examples
Literal characters
Sr.No. | Example & Description |
---|---|
1 | pythonMatch “python”. |
Character classes
Sr.No. | Example & Description |
---|---|
1 | [Pp]ythonMatch “Python” or “python” |
2 | rub[ye]Match “ruby” or “rube” |
3 | [aeiou]Match any one lowercase vowel |
4 | [0-9]Match any digit; same as [0123456789] |
5 | [a-z]Match any lowercase ASCII letter |
6 | [A-Z]Match any uppercase ASCII letter |
7 | [a-zA-Z0-9]Match any of the above |
8 | [^aeiou]Match anything other than a lowercase vowel |
9 | [^0-9]Match anything other than a digit |
Special Character Classes
Sr.No. | Example & Description |
---|---|
1 | .Match any character except newline |
2 | \dMatch a digit: [0-9] |
3 | \DMatch a nondigit: [^0-9] |
4 | \sMatch a whitespace character: [ \t\r\n\f] |
5 | \SMatch nonwhitespace: [^ \t\r\n\f] |
6 | \wMatch a single word character: [A-Za-z0-9_] |
7 | \WMatch a nonword character: [^A-Za-z0-9_] |
Repetition Cases
Sr.No. | Example & Description |
---|---|
1 | ruby?Match “rub” or “ruby”: the y is optional |
2 | ruby*Match “rub” plus 0 or more ys |
3 | ruby+Match “rub” plus 1 or more ys |
4 | \d{3}Match exactly 3 digits |
5 | \d{3,}Match 3 or more digits |
6 | \d{3,5}Match 3, 4, or 5 digits |
Nongreedy repetition
This matches the smallest number of repetitions −
Sr.No. | Example & Description |
---|---|
1 | <.*>Greedy repetition: matches “<python>perl>” |
2 | <.*?>Nongreedy: matches “<python>” in “<python>perl>” |
Grouping with Parentheses
Sr.No. | Example & Description |
---|---|
1 | \D\d+No group: + repeats \d |
2 | (\D\d)+Grouped: + repeats \D\d pair |
3 | ([Pp]ython(, )?)+Match “Python”, “Python, python, python”, etc. |
Backreferences
This matches a previously matched group again −
Sr.No. | Example & Description |
---|---|
1 | ([Pp])ython&\1ailsMatch python&pails or Python&Pails |
2 | ([‘”])[^\1]*\1Single or double-quoted string. \1 matches whatever the 1st group matched. \2 matches whatever the 2nd group matched, etc. |
Alternatives
Sr.No. | Example & Description |
---|---|
1 | python|perlMatch “python” or “perl” |
2 | rub(y|le))Match “ruby” or “ruble” |
3 | Python(!+|\?)“Python” followed by one or more ! or one ? |
Anchors
This needs to specify match position.
Sr.No. | Example & Description |
---|---|
1 | ^PythonMatch “Python” at the start of a string or internal line |
2 | Python$Match “Python” at the end of a string or line |
3 | \APythonMatch “Python” at the start of a string |
4 | Python\ZMatch “Python” at the end of a string |
5 | \bPython\bMatch “Python” at a word boundary |
6 | \brub\B\B is nonword boundary: match “rub” in “rube” and “ruby” but not alone |
7 | Python(?=!)Match “Python”, if followed by an exclamation point. |
8 | Python(?!!)Match “Python”, if not followed by an exclamation point. |
Special Syntax with Parentheses
Sr.No. | Example & Description |
---|---|
1 | R(?#comment)Matches “R”. All the rest is a comment |
2 | R(?i)ubyCase-insensitive while matching “uby” |
3 | R(?i:uby)Same as above |
4 | rub(?:y|le))Group only without creating \1 backreference |
Python Regular Expression Functions
We have a few functions to help us use Python regex.
1. match()
match() takes two arguments- a pattern and a string. If they match, it returns the string. Else, it returns None. Let’s take a few Python regular expression match examples.
>>> print(re.match('center','centre'))
Output
None>>> print(re.match('...\w\we','centre'))
Output
<_sre.SRE_Match object; span=(0, 6), match=’centre’>
2. search()
search(), like match(), takes two arguments- the pattern and the string to be searched. Let’s take a few examples.
>>> match=re.search('aa?yushi','ayushi')>>> match.group()
Output
‘ayushi’>>> match=re.search('aa?yushi?','ayush ayushi')>>> match.group()
Output
‘ayush’>>> match=re.search('\w*end','Hey! What are your plans for the weekend?')>>> match.group()
Output
‘weekend’>>> match=re.search('^\w*end','Hey! What are your plans for the weekend?')>>> match.group()
Output
Traceback (most recent call last):File “<pyshell#337>”, line 1, in <module>
Here, an AttributeError raised because it found no match. This is because we specified that this pattern should be at the beginning of the string. Let’s try searching for space.
>>> match=re.search('i\sS','Ayushi Sharma')>>> match.group()
Output
‘i S’>>> match=re.search('\w+c{2}\w*','Occam\'s Razor')>>> match.group()
Output
‘Occam’
It really will take some practice to get it into habit what the metacharacters mean. But since we don’t have so many, this will hardly take an hour.
Python Regex Options
The functions we discussed may take an optional argument as well. These options are:
1. Python Regular Expression IGNORECASE
This Python Regex ignore case ignores the case while matching. Take this example of Python Regex IGNORECASE:
>>> match=re.findall(r'hi','Hi, did you ship it, Hillary?',re.IGNORECASE)>>> <strong>for</strong> i <strong>in</strong> match: print(i)
Output
Hihi
Hi
2. Python MULTILINE
Working with a string of multiple lines, this allows ^ and $ to match the start and end of each line, not just the whole string.
>>> match=re.findall(r'^Hi','Hi, did you ship it, Hillary?\nNo, I didn\'t, but Hi',re.MULTILINE)>>> <strong>for</strong> i <strong>in</strong> match: print(i)
Output
Hi
3. Python DOTALL
.* does not scan everything in a multiline string; it only matches the first line. This is because . does not match a newline. To allow this, we use DOTALL.
>>> match=re.findall(r'.*','Hi, did you ship it, Hillary?\nNo, I didn\'t, but Hi',re.DOTALL)>>> <strong>for</strong> i <strong>in</strong> match: print(i)
Output
Hi, did you ship it, Hillary?No, I didn’t, but Hi
Greedy vs Non-Greedy
The metacharacters *, +, and ? are greedy. This means that they keep searching. Let’s take an example.
>>> match=re.findall(r'(<.*>)','<em>Strong</em> <i>Italic</i>')>>> <strong>for</strong> i <strong>in</strong> match: print(i)
Output
<em></em> <i> </i>
This gave us the whole string, because it greedily keeps searching. What if we just want the opening and closing tags? Look:
print(i)>>> match=re.findall(r'(<.*?>)','<em>Strong</em> <i>Italic</i>')>>> <strong>for</strong> i <strong>in</strong> match: print(i)
Output
<em></em> <i> </i>
The .* is greedy, and the ? makes it non-greedy.
Alternatively, we could also do this:>>> match=re.findall(r'</?\w+>’,'<em>Strong</em> <i>Italic</i>’)>>> for i in match: print(i)
Output
<em></em> <i> </i>
Here’s another example:
>>> match=re.findall('(a*?)b','aaabbc')>>> <strong>for</strong> i <strong>in</strong> match: print(i)
Output
aaa
Here, the ? makes * non-greedy. Also, if we would have skipped the b after the ?, it would have returned an empty string. The ? here needs a character after it to stop at. This works for all three- *?, +?, and ??. Similarly, {m,n}? makes it non-greedy, and matches as few occurrences as possible.
Substitution
We can use the sub() function to substitute the part of a string with another. sub() takes three arguments- pattern, substring, and string.>>> re.sub(‘^a’,’an’,’a apple’)
Output‘
an apple’
Here, we used ^ so it won’t change apple to anpple. The grammar police approve.
Python Regex Applications
So, we learned so much about Python regular expressions, but where do we use them? They find use in these places:
- Search engines
- Find and Replace dialogues of word processor and text editors
- Text processing utilities like sed and AWK
- Lexical analysis