Python re module: find, extract and replace text with regex
re finds, matches, splits and replaces text using patterns, and shows what happens when a pattern matches, does not match or is invalid.
Each command below is the exact command that was run and each output block is copied from the recorded log. Test ids, log hashes and exit statuses are in the evidence table.
TE-56-A: search matches anywhere, match anchors at the start, and no match returns None rather than raising
No setup needed: the command below creates what it uses in the throwaway folder.
Command:
python3 -c 'import re; s="abc123"; print(bool(re.search(r"\d+", s)), bool(re.match(r"\d+", s))); print(re.findall(r"\d", s), re.sub(r"\d","#",s), re.split("b", s)); print(re.search("zzz", s))'Recorded output:
True False
['1', '2', '3'] abc### ['a', 'c123']
NoneTE-56-B: groups in a pattern give the matched parts, and span gives the start and end positions
Setup for this run, inside a throwaway directory (mktemp -d):
cat > s.py <<'EOS'
import re
m = re.search(r'(\d+)-(\d+)', 'tel 12-345')
print(m.group(0), m.group(1), m.group(2), m.span())
EOSCommand:
python3 s.pyRecorded output:
12-345 12 345 (4, 10)Exit status: 0. The output matches the expected text written before the run.
TE-56-C: expected non-zero exit: a pattern with an unclosed group raises re.error
Setup for this run, inside a throwaway directory (mktemp -d):
cat > s.py <<'EOS'
import re
re.compile('(')
EOSThe file ../err.txt sits one level above the working folder, inside the same throwaway tree. The output block below shows only the last line of the error; the full traceback went to ../err.txt.
Command:
python3 s.py 2>../err.txt; rc=$?; tail -n 1 ../err.txt; exit $rcRecorded output:
re.error: missing ), unterminated subpattern at position 0Exit status: 1. This status and the output match what was written before the run.
Reading the recorded output
The first line shows two checks on the text abc123. re.search found digits and gave True. re.match gave False, because the text starts with letters and match looks only at the start. The second line has three values: findall with a single digit pattern gave ['1', '2', '3'], sub replaced each digit with # and gave abc###, and split on the letter b gave ['a', 'c123']. The third line shows that a search for zzz printed None, with no error.
TE-56-B: on tel 12-345, group(0) is 12-345, group(1) is 12 and group(2) is 345, and span() is (4, 10), the start and end positions in the text. TE-56-C: compiling the pattern ( stopped with re.error: missing ), unterminated subpattern at position 0, with exit status 1. The error names the position.
Limits
search matches anywhere, match anchors at the start; no match returns None.
Source
Evidence
TE-56-A: search matches anywhere, match anchors at the start, and no match returns None rather than raising
- logs: one recorded log for each row above
- Recorded on: Linux x86_64, kernel 7.3.4; bash 5.1.16; git 2.34.1; python 3.10.12; findutils 4.8.0; grep 3.7; sed 4.8; gawk 5.1.0; tar 1.34; curl 7.81.0. Locale C.UTF-8, TZ=UTC.
Evidence table
| Test | What it checks | Exit | Verdict | Log sha256 (first 12) | |---|---|---|---|---| | TE-56-A | search matches anywhere, match anchors at the start, and no match returns None rather than raising | 0 | PASS | 1786c4c7ed7d | | TE-56-B | groups in a pattern give the matched parts, and span gives the start and end positions | 0 | PASS | c162718c0c08 | | TE-56-C | expected non-zero exit: a pattern with an unclosed group raises re.error | 1 | PASS | 38d7bb8759d7 |
Every row has a recorded log holding the setup commands, the command, stdout, stderr, exit status and the final directory state. The first 12 characters of the log's sha256 are in the table.
All full guides: All guides