Regular expressions
A regex describes a pattern of text. You need four functions — search (first match anywhere), match (at the start), findall (every match), sub (replace) — and about a dozen symbols. Always write patterns as raw strings (r"…") so backslashes reach the regex engine intact.
You should see
user=ada id=4021 | ada 4021
{'date': '2026-09-20', 'time': '14:03:11', 'level': 'ERROR'}
['10.0.0.7']
['user', 'id', 'msg', 'ip']
2026-09-20 14:03:11 ERROR user=ada id=**
due 20/09/2026
'[email protected]' True
'not an email' False
'[email protected]' True
['a', 'b', 'c', 'd']#hashtag from "loving #python and #regex, not #c++ though". Then make the pattern stop at the +.| Symbol | Matches | Example |
|---|---|---|
. | any character except newline | a.c → abc, a-c |
\d \w \s | digit · word char (letters, digits, _) · whitespace | capitals negate: \D = not a digit |
* + ? | 0 or more · 1 or more · 0 or 1 | colou?r → color, colour |
{n} {n,m} | exactly n · between n and m | \d{4} → a year |
[abc] [^abc] [a-z] | a set · not in the set · a range | |
^ $ | start · end of string (or line with re.M) | |
(…) (?P<name>…) | a group · a named group | m.group(1), m["name"] |
a|b | a or b | cat|dog |
*? +? | lazy: as few as possible | <.+?> matches one tag, not the whole line |
<.+> on <b>hi</b> matches the whole thing, because + grabs as much as it can and still succeeds. Add ? for the shortest match. And do not parse HTML with regex at all — use an HTML parser.Which call finds a pattern anywhere in the string, not just at the start?
match is anchored to position 0 and fullmatch must consume the whole string. Using match when you meant search is the most common regex bug.