#! /usr/bin/python3 # import sys # for x in sys.modules: print (x) # sys.path ## ------------------------- ## regular expressions ## ------------------------- import re def retest(str = 'an example word:cat!!'): pat = 'word:\w\w\w' match = re.search(pat, str) # If-statement after search() tests if it succeeded if match: print ('found: {}'.format( match.group())) else: print ('Did not find {} in {}'.format(pat, str)) patterns = ['aaa', # contains aaa 'abc', # contains abc '...', # contains three characters '^...$', # starts with three characters '\.\.\.', # contains three periods 'abd', # contains abd '^[aeiou]*$', # contains only vowels '^[^aeiou]*$', # contains only NON vowels '\w\W\w', # two word characters separated by a non-word char '\w\w\w', # three word characters '^\d+$', # contains only decimal digits '^[0-7]+$', # contains only octal digits '^[0-9A-Fa-f]+$', # contains only hexadecimal digits '^[a-z]*$', # contains only lower case letters '^\s+', # starts with a whitespace char '^\d\s?', # starts with a digit followed by zero or one space '\w+@\w+', # match email address '(b)*(a)*(c)', '^b*a*c$', '^(b|c)*(a|b)*$', '^bb*(ab|ba)*|(bbc|cbc)*$', '^(ab|ba|cb|bc|ca|ac)*$', '(bc|bcc)(bac|cba)(cba|aa)', '\AThe', # \A is beginning of string ] def retest2(patlist = patterns): while (True): str = input("Enter string: ") if str == "quit": break for pat in patlist: match = re.search(pat, str) # match = re.match(pat, str) if match: print ('Matched: {} with: {}'.format(pat, match.group(0))) else: print ('Did not find {} in {}'.format(pat, str)) ## group matching def retest3(patlist = ['(\w+)@(\w+)', '^(\d\d\d).(\d\d\d)']): while (True): str = input("Enter string: ") if str == "quit": break for pat in patlist: match = re.search(pat, str) if match: print ('Matched: {} group 1: {} group 2: {}'.format(pat, match.group(1), match.group(2))) else: print ('Did not find {} in {}'.format(pat, str)) # findall def retest4(patlist = patterns): while (True): str = input("Enter string: ") if str == "quit": break for pat in patlist: match = re.findall(pat, str) if match: print ('Total matches for {}: {}'.format(pat, len(match))) else: print ('Did not find {} in {}'.format(pat, str)) ##---------------------------------------------