Lookahead and lookbehind

Zero-width checks that print booleans.

Lookahead (?=...) and lookbehind (?<=...) check neighbors without consuming them. Print booleans — keep the strings tiny.

Goal

Check a password-like token and a shilling amount that follows KES.

import re
print(bool(re.search(r"(?=.*[A-Z])(?=.*\d).{6,}", "Nai12x")))
print(bool(re.search(r"(?=.*[A-Z])(?=.*\d).{6,}", "nairobi")))
import re
print(re.findall(r"(?<=KES )\d+", "total KES 126 and KES 73"))
import re
print(re.findall(r"\d+(?= units)", "12 units and 9 boxes"))
import re
print(bool(re.search(r"(?<!\d)12(?!\d)", "x12y")))
print(bool(re.search(r"(?<!\d)12(?!\d)", "x123y")))