import re
# import regex # if you like good times
# intended to replace `re`, the regex module has many advanced
# features for regex lovers. http://p...content-available-to-author-only...n.org/pypi/regex
pattern = r'(\w+):(\w+):(\d+)'
subject = 'apple:green:3 banana:yellow:5'
regex = re.compile(pattern)
######## The six main tasks we're likely to have ########
# Task 1: Is there a match?
print("*** Is there a Match? ***")
if regex.search(subject):
print ("Yes")
else:
print ("No")
# Task 2: How many matches are there?
print("\n" + "*** Number of Matches ***")
matches = regex.findall(subject)
print(len(matches))
# Task 3: What is the first match?
print("\n" + "*** First Match ***")
match = regex.search(subject)
if match:
print("Overall match: ", match.group(0))
print("Group 1 : ", match.group(1))
print("Group 2 : ", match.group(2))
print("Group 3 : ", match.group(3))
# Task 4: What are all the matches?
print("\n" + "*** All Matches ***\n")
print("------ Method 1: finditer ------\n")
for match in regex.finditer(subject):
print ("--- Start of Match ---")
print("Overall match: ", match.group(0))
print("Group 1 : ", match.group(1))
print("Group 2 : ", match.group(2))
print("Group 3 : ", match.group(3))
print ("--- End of Match---\n")
print("\n------ Method 2: findall ------\n")
# if there are capture groups, findall doesn't return the overall match
# therefore, in that case, wrap the pattern in capturing parentheses
# the overall match becomes group 1, so other group numbers are bumped up!
wrappedpattern = "(" + pattern + ")"
wrappedregex = re.compile(wrappedpattern)
matches = wrappedregex.findall(subject)
if len(matches)>0:
for match in matches:
print ("--- Start of Match ---")
print ("Overall Match: ",match[0])
print ("Group 1: ",match[1])
print ("Group 2: ",match[2])
print ("Group 3: ",match[3])
print ("--- End of Match---\n")
# Task 5: Replace the matches
# simple replacement: reverse group
print("\n" + "*** Replacements ***")
print("Let's reverse the groups")
def reversegroups(m):
return m.group(3) + ":" + m.group(2) + ":" + m.group(1)
replaced = regex.sub(reversegroups, subject)
print(replaced)
# Task 6: Split
print("\n" + "*** Splits ***")
# Let's split at colons or spaces
splits = re.split(r":|\s",subject)
for split in splits:
print (split)