re.search() vs re.match()
re.search() and re.match() are both Python Regex Module APIs, and choosing between them is a common point of confusion. This page compares their syntax, parameters, return values, and typical use cases so you can pick the right one. re.search(): Scans through the string looking for a match to the regex pattern. Returns the first match or None. Its syntax is re.search(pattern, string, flags=0), and it returns match object or None re.match(): Checks for a match only at the beginning of the string. Its syntax is re.match(pattern, string, flags=0), and it returns match object or None Example of re.search(): m = re.search(r"\\d+", "abc123def")
m.group() # "123"
m.start() # 3. Example of re.match(): re.match(r"\\d+", "123abc") # Match (at start)
re.match(r"\\d+", "abc123") # None (not at start). Read the full references for re.search() and re.match() to see parameters, edge cases, and example code side by side. Other Regex Module APIs worth comparing: re.fullmatch(), re.findall(), re.finditer(), re.sub(), re.subn(), re.split().