Friday, March 31, 2017

Python 3 - Regular Expressions

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 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. Nevertheless, 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'.

Basic patterns that match single chars

S.No. Expression & Matches
1 a, X, 9, <
ordinary characters just match themselves exactly.
2 . (a period)
matches any single character except newline '\n'
3 \w
matches a "word" character: a letter or digit or underbar [a-zA-Z0-9_].
4 \W
matches any non-word character.
5 \b
boundary between word and non-word
6 \s
matches a single whitespace character -- space, newline, return, tab
7 \S
matches any non-whitespace character.
8 \t, \n, \r
tab, newline, return
9 \d
decimal digit [0-9]
10 ^
matches start of the string
11 $
match the end of the string
12 \
inhibit the "specialness" of a character.

Compilation flags

Compilation flags let you modify some aspects of how regular expressions work. Flags are available in the re module under two names, a long name such as IGNORECASE and a short, one-letter form such as I.
S.No. Flag & Meaning
1 ASCII, A
Makes several escapes like \w, \b, \s and \d match only on ASCII characters with the respective property.
2 DOTALL, S
Make, match any character, including newlines
3 IGNORECASE, I
Do case-insensitive matches
4 LOCALE, L
Do a locale-aware match
5 MULTILINE, M
Multi-line matching, affecting ^ and $
6 VERBOSE, X (for ‘extended’)
Enable verbose REs, which can be organized more cleanly and understandably

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 −
S.No.Parameter & Description
1 pattern
This is the regular expression to be matched.
2 string
This is the string, which would be searched to match the pattern at the beginning of string.
3 flags
You 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.
S.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/python3
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 the 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 −
S.No.Parameter & Description
1pattern
This is the regular expression to be matched.
2string
This is the string, which would be searched to match the pattern anywhere in the string.
3flags
You 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 the matched expression.
S.No.Match Object Method & Description
1group(num = 0)
This method returns entire match (or specific subgroup num)
2groups()
This method returns all matching subgroups in a tuple (empty if there weren't any)

Example

#!/usr/bin/python3
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 the following result −
matchObj.group() :  Cats are smarter than dogs
matchObj.group(1) :  Cats
matchObj.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/python3
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 --> matchObj.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 is provided. This method returns modified string.

Example

#!/usr/bin/python3
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 −
S.No.Modifier & Description
1re.I
Performs case-insensitive matching.
2re.L
Interprets words according to the current locale. This interpretation affects the alphabetic group (\w and \W), as well as word boundary behavior (\b and \B).
3re.M
Makes $ 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).
4re.S
Makes a period (dot) match any character, including a newline.
5re.U
Interprets letters according to the Unicode character set. This flag affects the behavior of \w, \W, \b, \B.
6re.X
Permits "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 the control characters, (+ ? . * ^ $ ( ) [ ] { } | \), all characters match themselves. You can escape a control character by preceding it with a backslash.
The following table lists the regular expression syntax that is available in Python −

Regular Expression Examples

Literal characters

S.No.Example & Description
1python
Match "python".

Character classes

S.No.Example & Description
1[Pp]ython
Match "Python" or "python"
2rub[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

S.No.Example & Description
1.
Match any character except newline
2\d
Match a digit: [0-9]
3\D
Match a nondigit: [^0-9]
4\s
Match a whitespace character: [ \t\r\n\f]
5\S
Match nonwhitespace: [^ \t\r\n\f]
6\w
Match a single word character: [A-Za-z0-9_]
7\W
Match a nonword character: [^A-Za-z0-9_]

Repetition Cases

S.No.Example & Description
1ruby?
Match "rub" or "ruby": the y is optional
2ruby*
Match "rub" plus 0 or more ys
3ruby+
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 −
S.No.Example & Description
1<.*>
Greedy repetition: matches "<python>perl>"
2<.*?>
Nongreedy: matches "<python>" in "<python>perl>"

Grouping with Parentheses

S.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 −
S.No.Example & Description
1([Pp])ython&\1ails
Match python&pails or Python&Pails
2(['"])[^\1]*\1
Single or double-quoted string. \1 matches whatever the 1st group matched. \2 matches whatever the 2nd group matched, etc.

Alternatives

S.No.Example & Description
1python|perl
Match "python" or "perl"
2rub(y|le)
Match "ruby" or "ruble"
3Python(!+|\?)
"Python" followed by one or more ! or one ?

Anchors

This needs to specify match position.
S.No.Example & Description
1^Python
Match "Python" at the start of a string or internal line
2Python$
Match "Python" at the end of a string or line
3\APython
Match "Python" at the start of a string
4Python\Z
Match "Python" at the end of a string
5\bPython\b
Match "Python" at a word boundary
6\brub\B
\B is nonword boundary: match "rub" in "rube" and "ruby" but not alone
7Python(?=!)
Match "Python", if followed by an exclamation point.
8Python(?!!)
Match "Python", if not followed by an exclamation point.

Special Syntax with Parentheses

S.No.Example & Description
1R(?#comment)
Matches "R". All the rest is a comment
2R(?i)uby
Case-insensitive while matching "uby"
3R(?i:uby)
Same as above
4rub(?:y|le))
Group only without creating \1 backreference

6 comments:

  1. Excellent post, thanks for this. I gathered lots of information from this and I am happy about it. Do share more updates.
    We as a team of real-time industrial experience with a lot of knowledge in developing applications in Ethical Hacking , Certified Ethical hacking, python programming , aws training , ccna training (7+ years) will ensure that we will deliver our best in python training in chennai. , and we believe that no one matches us in this context.

    ccna training in chennai
    aws training in chennai
    aws devops training in chennai
    python training
    python training in chennai
    python training
    ceh training in chennai
    ethical hacking course in chennai
    ethical hacking training in chennai

    ReplyDelete
  2. This is a fantastic tutorial on regular expressions in Python 3!.
    Thanks for sharing it with us.
    Python Course in Nagpur

    ReplyDelete
  3. Thanks for sharing this here. Great Post, really it was very helpful for us. I found this blog to be very useful! python classes in satara

    ReplyDelete