import re text = "this is a bunch of things ab3e ab12e abc2e abc12e abc2ed ab12ed" # computingID_regex = re.compile(r"[a-z]{2,3}[0-9][a-z]{1,2}") computingID_regex = re.compile(r"\w{2,3}\d\w{1,2}") print("=== example: search() ===") # search() returns the first occurrence of that matched object # including start and end indices, and the matched string search_result = computingID_regex.search(text) print(search_result) print(type(search_result)) # # group() returns the matched object print(search_result.group()) print(type(search_result.group())) # # start() returns the first index of the match print(search_result.start()) print(type(search_result.start())) # # end() returns the last index of the match print(search_result.end()) print(type(search_result.end())) # print("=== example: match() ===") # match() checks if the pattern exists at the beginning of a given string # returns None if pattern not found (python processes it as False) # otherwise, returns the matched object # thus supporting group(), start(), and end() match_result = computingID_regex.match(text) print(match_result) print("=== example: findall() ===") # findall() returns a list of strings of all non-overlapping matches # of the pattern in a given string findall_result = computingID_regex.findall(text) print(findall_result) print(type(findall_result)) # print("--- loop to print result from findall() ---") for str in findall_result: print(str) print("=== example: finditer() ===") # finditer() returns a list (or iterator) of matched objects # thus supporting group(), start(), and end() finditer_result = computingID_regex.finditer(text) print(finditer_result) print(type(finditer_result)) # print("--- loop print result from finditer() ---") for obj in finditer_result: print(obj, obj.group(), obj.start(), obj.end())