# ================================================================== # regular expressions # # Note: make search patterns raw strings (see Python string literals) # ================================================================== import re # ------------------------------------------------------------------ # for speed/efficiency use a compiled regular expression # when search many strings # ------------------------------------------------------------------ print('') str = '.db.abcxyz.db' ##pat = re.compile('\.db$',re.IGNORECASE) pat = r'\.db$' p = re.compile(pat) m = p.search(str) print('Pat Obj: {}'.format(p)) print('pattern: {}\nLen: {}'.format(m,pat,len(pat))) if m: print('Match ({})'.format(str)) else: print('No Match ({})'.format(str)) # ------------------------------------------------------------------- # when searching one or two strings, just do a search # ------------------------------------------------------------------- print('') m = re.search(pat,str) print('Pattern: {}\nLen: {}'.format(pat,len(str))) if m: print('Match ({})'.format(str)) else: print('No Match ({})'.format(str))