CKTIS / Python
re.search
Scans for the first position at which the pattern matches.
Version: Python 3.14; repository image, production runtime not attested
Syntax
re.search(pattern, string, flags=0)Scans for the first position at which the pattern matches.
A failed search returns None, so inspect the result before accessing a capture or span.
Scan the string for the earliest starting position where the pattern can succeed. Return the corresponding Match or None. A zero-width match is a successful result. In a12b3 the first run of digits is 12, with span (1, 3); the later 3 is not returned. Check the result before calling group().
import re
s = "b12b3"
m = re.search(r"\d+", s)
assert m and m.span() == (1, 3) and m.group() == "12"
assert re.search("z*", s).span() == (0, 0)
assert re.search("z+", s) is None
Examples
import re
print(re.search(r"\d+", "a12b3").span())
print(re.search(r"\d+", "abc"))
Result
(1, 3) None
Scan the string for the earliest starting position where the pattern can succeed. Return the corresponding Match or None. A zero-width match is a successful result. In a12b3 the first run of digits is 12, with span (1, 3); the later 3 is not returned. Check the result before calling group().