A regular expression, or regex, is a pattern for recognizing text and often extracting data from it. Python’s `re` module provides pattern searches when looking for one fixed substring is too limiting—for example, when the text must consist of digits or may have several valid spellings. section 16.1 section 6.4.4
Pass a pattern string to `re.compile()` to obtain a compiled regex, then call its `search()` method on text. A successful search returns a match object; an unsuccessful one returns `None`. Compiling is optional, but can improve speed when processing large amounts of text. section 16.1
import re
regexp = re.compile("hello")
count = 0
file = open("textfile", 'r')
for line in file.readlines():
if regexp.search(line):
count = count + 1
file.close()
print(count)
This file-processing example searches each line for `hello`. It counts a line once even if `hello` occurs more than once on that line. section 16.1
Metacharacters let one pattern describe several possible strings:
- `hello|Hello` matches either spelling; `(h|H)ello` limits the choice to the first letter. section 16.2
- `[hH]ello` matches either first letter using a character class. A range such as `[a-z]` matches one character in that range. section 16.2
- `\d` indicates a digit; `\D` can identify nondigits to remove before reformatting text. section 16.7 section 16.6.2
Prefer raw strings for patterns containing backslashes: prefix the Python string with `r` so Python does not process its escape sequences before `re` receives them. Otherwise, a pattern intended to match a literal backslash followed by `ten` can instead search for a tab followed by `en`. section 16.3 section 16.7
Normalize phone numbers
A phone-number formatter can use `re.sub(r'\D', '', phone_number)` to keep only digits, check the resulting length and number components, then use another substitution to add separators. Matching only expected separator patterns is another approach, but misses formats it did not anticipate. Test both numbers that should match and numbers that must fail; do not assume AI-generated patterns or tests are correct. section 16.6.2 section 16.6.1


























