Regular Expressions
Presented by Prof. Nikunja Parida
Regular Expressions
In computing, a regular expression, also
referred to as “regex” or “regexp”, provides a
concise and flexible means for matching
strings of text, such as particular characters,
words, or patterns of characters. A regular
expression is written in a formal language
that can be interpreted by a regular
expression processor.
Regular Expressions
Really clever “wild card” expressions for matching
and parsing strings
Import re
RE - Functions
[Link](pattern,flag): Function to create a regular expression object.
[Link](pattern,string,flag): Checks for a match only at the beginning of the string.
[Link](pattern,string,flag): Search entire string for a match to the pattern.
[Link](pattern,string,flag): Finds all substrings where the regex pattern matches and
returns them as a list.
[Link](pattern, repl, string, count=0, flags=0): Replaces occurrences of the regex pattern
with another string.
[Link](pattern, string, maxsplit=0, flags=0): split a string into a list of substrings based on a
specified regular expression pattern as the delimiter.
flag: re. IGNORECASE , re. MULTILINE, re. DOTALL, re. VERVOSE
RE- Special Characters
^ Matches the beginning of a line
$ Matches the end of the line
. Matches any character
\s Matches whitespace
\S Matches any non-whitespace character
* Repeats a character zero or more times
? Matches zero or one repetition of the preceding RE
*? Repeats a character zero or more times (non-greedy)
+ Repeats a character one or more times
+? Repeats a character one or more times (non-greedy)
[aeiou] Matches a single character in the listed set
[^XYZ] Matches a single character not in the listed set
[a-z0-9] The set of characters can include a range
( Indicates where string extraction is to start
) Indicates where string extraction is to end
{m} Specifies that exactly m copies of the previous RE
should be matched
{m,n} Matches from m to n repetitions of the preceding RE,
attempting to match as many repetitions as possible
Special Sequences
\d: Matches any digit (0-9). Equivalent to [0-9].
\D: Matches any non-digit character. Equivalent to [^0-9].
\w: Matches any alphanumeric character (letters, digits, and underscore).
Equivalent to [a-zA-Z0-9_].
\W: Matches any non-alphanumeric character.
Equivalent to [^a-zA-Z0-9_].
\s: Matches any whitespace character (space, tab, newline, carriage return, form feed, vertical
tab). Equivalent to [ \t\n\r\f\v].
\S: Matches any non-whitespace character. Equivalent to [^ \t\n\r\f\v].
Raw String
A raw string in the context of regular expressions (regex), particularly in
Python, is a string literal prefixed with an ‘r’. Its primary purpose is to
prevent Python's default interpretation of backslash escape sequences
within the string.
In regex, backslashes are also used for special sequences (e.g., \d for
digits, \w for word characters) and to escape special regex characters
(e.g., \. to match a literal dot).
Without raw strings, you would need to double-escape backslashes in
your regex patterns (e.g., \\d to match a digit) to prevent Python from
interpreting them first. Raw strings eliminate this need, allowing you to
write r"\d" directly
Even Cooler Regex Version
From [Link]@[Link] Sat Jan 5 09:14:16 2008
import re
lin = 'From [Link]@[Link] Sat Jan 5 09:14:16 2008'
y = [Link]('^From .*@([^ ]*)',lin)
print(y)
['[Link]']
'^From .*@([^ ]+)'
Stop extracting
Wild-Card Characters
• The dot character matches any character
• If you add the asterisk character, the character is “any
number of times”
Many
Match the start of
times
X-Sieve: CMU Sieve 2.3 the line
X-DSPAM-Result: Innocent
X-DSPAM-Confidence: 0.8475
X-Content-Type-Message-Body: text/plain
^X.*:
Match any character
Fine-Tuning Your Match
Depending on how “clean” your data is and the purpose of
your application, you may want to narrow your match down
a bit
Many
Match the start times
X-Sieve: CMU Sieve 2.3 of the line
X-DSPAM-Result: Innocent
X-Plane is behind schedule: two weeks
X-: Very short
^X.*:
Match any character
Fine-Tuning Your Match
Depending on how “clean” your data is and the purpose of
your application, you may want to narrow your match down
a bit
One or more
Match the start
X-Sieve: CMU Sieve 2.3 times
X-DSPAM-Result: Innocent of the line
X-: Very Short
X-Plane is behind schedule: two weeks ^X-\S+:
Match any non-whitespace
character
Matching and Extracting
Data
• [Link]() returns a True/False depending on whether
the string matches the regular expression
• If we actually want the matching strings to be extracted,
we use [Link]()
>>> import re
[0-9]+ >>> x = 'My 2 favorite numbers are 19 and 42'
>>> y = [Link]('[0-9]+',x)
>>> print(y)
One or more ['2', '19', '42']
digits
Matching and Extracting
Data
When we use [Link](), it returns a list of zero or more
sub-strings that match the regular expression
>>> import re
>>> x = 'My 2 favorite numbers are 19 and 42'
>>> y = [Link]('[0-9]+',x)
>>> print(y)
['2', '19', '42']
>>> y = [Link]('[AEIOU]+',x)
>>> print(y)
[]
Warning: Greedy Matching
The repeat characters (* and +) push outward in both
directions (greedy) to match the largest possible string
One or more
characters
>>> import re
>>> x = 'From: Using the : character'
>>> y = [Link]('^F.+:', x)
>>> print(y) ^F.+:
['From: Using the :']
First character in Last character in
Why not 'From:' ?
the match is an F the match is a :
Non-Greedy Matching
Not all regular expression repeat codes are
greedy! If you add a ? character, the + and * One or more
chill out a bit... characters
but not
>>> import re greedy
>>> x = 'From: Using the : character'
>>> y = [Link]('^F.+?:', x) ^F.+?:
>>> print(y)
['From:']
First character in Last character in
the match is an F the match is a :
Fine-Tuning String Extraction
You can refine the match for [Link]() and separately determine
which portion of the match is to be extracted by using parentheses
From [Link]@[Link] Sat Jan 5 09:14:16 2008
>>> y = [Link]('\S+@\S+',x) \S+@\S+
>>> print(y)
['[Link]@[Link]’] At least one
non-
whitespace
character
Fine-Tuning String Extraction
Parentheses are not part of the match - but they tell where
to start and stop what string to extract
From [Link]@[Link] Sat Jan 5 09:14:16 2008
>>> y = [Link]('\S+@\S+',x)
>>> print(y)
['[Link]@[Link]']
^From (\S+@\S+)
>>> y = [Link]('^From (\S+@\S+)',x)
>>> print(y)
['[Link]@[Link]']
2 3
1 1
From [Link]@[Link] Sat Jan 5 09:14:16 2008
>>> data = 'From [Link]@[Link] Sat Jan 5 09:14:16 2008'
>>> atpos = [Link]('@')
>>> print(atpos)
21 Extracting a host
>>> sppos = [Link](' ',atpos) name - using
>>> print(sppos)
31 find and string
>>> host = data[atpos+1 : sppos] slicing
>>> print(host)
[Link]
The Double Split Pattern
Sometimes we split a line one way, and then grab one of
the pieces of the line and split that piece again
From [Link]@[Link] Sat Jan 5 09:14:16 2008
words = [Link]() [Link]@[Link]
email = words[1] ['[Link]', '[Link]']
pieces = [Link]('@')
print(pieces[1]) '[Link]'
The Regex Version
From [Link]@[Link] Sat Jan 5 09:14:16 2008
import re
lin = 'From [Link]@[Link] Sat Jan 5 09:14:16 2008'
y = [Link]('@([^ ]*)',lin)
print(y)
['[Link]']
'@([^ ]*)'
Look through the string until you find an at
sign
The Regex Version
From [Link]@[Link] Sat Jan 5 09:14:16 2008
import re
lin = 'From [Link]@[Link] Sat Jan 5 09:14:16 2008'
y = [Link]('@([^ ]*)',lin)
print(y)
['[Link]']
'@([^ ]*)'
Match non-blank
Match many of them
character
The Regex Version
From [Link]@[Link] Sat Jan 5 09:14:16 2008
import re
lin = 'From [Link]@[Link] Sat Jan 5 09:14:16 2008'
y = [Link]('@([^ ]*)',lin)
print(y)
['[Link]']
'@([^ ]*)'
Extract the non-blank characters
Even Cooler Regex Version
From [Link]@[Link] Sat Jan 5 09:14:16 2008
import re
lin = 'From [Link]@[Link] Sat Jan 5 09:14:16 2008'
y = [Link]('^From .*@([^ ]*)',lin)
print(y)
['[Link]']
'^From .*@([^ ]*)'
Starting at the beginning of the line, look for the string
'From '
Even Cooler Regex Version
From [Link]@[Link] Sat Jan 5 09:14:16 2008
import re
lin = 'From [Link]@[Link] Sat Jan 5 09:14:16 2008'
y = [Link]('^From .*@([^ ]*)',lin)
print(y)
['[Link]']
'^From .*@([^ ]*)'
Skip a bunch of characters, looking for an at sign
Even Cooler Regex Version
From [Link]@[Link] Sat Jan 5 09:14:16 2008
import re
lin = 'From [Link]@[Link] Sat Jan 5 09:14:16 2008'
y = [Link]('^From .*@([^ ]*)',lin)
print(y)
['[Link]']
'^From .*@([^ ]*)'
Start extracting
Even Cooler Regex Version
From [Link]@[Link] Sat Jan 5 09:14:16 2008
import re
lin = 'From [Link]@[Link] Sat Jan 5 09:14:16 2008'
y = [Link]('^From .*@([^ ]*)',lin)
print(y)
['[Link]']
'^From .*@([^ ]+)'
Match non-blank Match many of
character them
Escape Character
If you want a special regular expression character to just
behave normally (most of the time) you prefix it with '\'
>>> import re
At least
>>> x = 'We just received $10.00 for cookies.' one or
>>> y = [Link]('\$[0-9.]+',x) more
>>> print(y)
['$10.00']
\$[0-9.]+
A real dollar A digit or
sign period
re - Functions
[Link](): Function to create a regular expression object.
import re
pattern = [Link](r"\d+")
string = "123456"
result = [Link](string)
Print(result)
<[Link] object; span=(0, 6), match='123456'>lt)
if result:
print([Link]())
Phone Number -Validation
Consider a scenario where you need to validate a list of phone numbers
to ensure they follow the pattern – (xxx) xxx-xxxx:
import re
pattern = r'\(\d{3}\) \d{3}-\d{4}'
text = "(123) 456-7890"
match = [Link](pattern, text)
if match:
print("Valid phone number.")
else:
print("Invalid phone number.")
Date-Validation
Consider a scenario where you need to validate a date to ensure they
follow the pattern – dd/mm/yyyy:
import re
text = "My email id is abc@[Link] which i have created on
05/01/2021"
pattern = ‘[0-9]{1,2}\/[0-9]{1,2}\/[0-9]{4}’
match = [Link](pattern, text)
print(match)
print("Invalid phone number.")
Email-Validation
Consider a scenario where you need to validate an email to ensure they follow the pattern –
#mira@[Link],mira@[Link]#mira_2023@[Link],123mira@[Link],Mira@[Link]#
[Link]@[Link]
import re
text = "My email id is abc@[Link] which i have created on 05/01/2021"
pattern = '[a-z0-9]+[\._]?[a-z0-9]+[@]\w+[.]\w{2,3}$'
match = [Link](pattern, text)
If math:
f”valid email
Else:
f” Not Valid