66.1.1. An introduction of regular expression/String pattern matching
(a) A regular expression, regex or regexp is a sequence of characters that define a search pattern.
66.1.2. Overview of a string pattern
(a) A regular expression, is called a pattern.
(b) An atom is a single point within the regex pattern which it tries to match to the target string.
(c) The simplest atom is a literal.
66.1.3. More on meta characters
(a) [ ] These are used for specifying a character class.
(c) * is used as a repeated qualifier for 0 or more times.
66.1.4. Pattern methods
(a) Pattern objects have several methods and attributes like match(), search() etc.
(c) The match() and search() return a None if no match is found.
(d) start() method on match object returns the starting position of the match.
66.1.5. A simple example on regex using findall() method
import re
mystring = "Hello!! Good Morning, Welcome to python tutorial class 24.For any queries please email to contactus@codetantra.com"
# write your code here
print(re.findall('^Hello', mystring))
print(re.findall('\d.', mystring))
print(re.findall('[a-c]+',mystring))
66.1.6. Example using the lisr for findall() function
import re
mystring = "Hello!! Good Morning, Welcome to python tutorial class 24."
matches = re.findall('[eo]+',mystring)# find all occcurences of e and o
for m in matches:
print(m)
# print m
66.1.7. Example to write a pattern to search email address
import re
a = input("string with email address: ")
z = re.findall("[\w+]@[\w+]",a)
if z:
print("email address:","['{}']".format(a))
else:
print("No email address found")
66.1.8. Compilation flags
]
(a) Compilation flags lets a user modify a few parts of regular expressions.
(b) IGNORECASE, I is used for case-insensitive matches.
66.1.9. Grouping
# impirt re
import re
mystring = "The alternate email address is victory@ct.com"
match = re.search('(\w+)@(\w+).(\w+)', mystring)
if match:
# find full match text and print the result
print("Full match:",match.group())
# find the match text corresponding to the 1st left parenthesis and print
print("Group 1:", match.group(1))
# find the match text corresponding to the 2nd left parenthesis and print
print("Group 2:", match.group(2))
# find the match text corresponding to the 3rd left parenthesis and print
print("Group 3:", match.group(3))
66.1.10. Grouping example
# write your code here
import re
d = input("data: ")
m = re.search('(\w+),(\w+),(\w+),(\w+)',d)
if m:
print("full:", m.group())
print("group 1:", m.group(1))
print("group 2:", m.group(2))
print("group 3:", m.group(3))
print("group 4:", m.group(4))
else:
print("you have not entered 4 words")