Explain a regex in plain English: what each part of a pattern does, with two worked examples
Python's debug output runs to 52 lines for a one-line password rule; Regex Workbench reads each part in words and names the groups of a log line.
We make Regex Workbench, the app this guide uses. The free methods come first; every step shown in Regex Workbench was checked in the app itself, and the screenshots come from the real app with invented sample data.
Short answer
Read a regular expression one part at a time: anchors, lookaheads, character sets, repeats and groups. Python's re.DEBUG lists the parts for free but in the engine's terms, 52 lines for a one-line password rule, and PowerShell's $Matches shows what each named group captured. Regex Workbench, which uses Python's re, writes every part in words next to the matches, says that \d also covers digits of other scripts, and warns that $ can leave a \r inside lines with Windows line ends.
Two patterns, part by part
A password rule with two lookaheads:
^(?=.*[A-Z])(?=.*\d)[A-Za-z\d!@#$%&*?]{8,}$
^the start of the line (with the multiline option; otherwise the start of the text).(?=.*[A-Z])a lookahead: somewhere ahead there is a capital letter A to Z. It is checked, not part of the match, so the engine is still at the start afterwards.(?=.*\d)a second lookahead: somewhere ahead there is a digit.[A-Za-z\d!@#$%&*?]{8,}8 or more characters, each an ASCII letter (A to Z or a to z), a digit or one of ! @ # $ % & * ?.$the end of the line.
On the five test lines sunflower, Sunflower7, SUNFLOWER7!, Sun7 and sunflower77!, it matches two: Sunflower7 and SUNFLOWER7!. The others lack a capital letter or are too short. The rule is an example for reading a pattern, not a password policy to copy: it accepts Sunflower7, a common word with one digit, and refuses spaces and hyphens, so it rules out passphrases such as "Sun flower 7".
A log-line pattern with named groups:
^(?P<date>\d{4}-\d{2}-\d{2}) (?P<level>ERROR|WARN)\s+\[(?P<module>[\w.]+)\] (?P<message>.*)$
(?P<date>\d{4}-\d{2}-\d{2})group 1, named date: four digits, a dash, two digits, a dash, two digits.(?P<level>ERROR|WARN)group 2, named level: one of two alternatives.\s+one or more whitespace characters (a tab matched too);\[and\]are the brackets themselves.(?P<module>[\w.]+)group 3: letters, digits, underscores and dots, such as billing.invoices.(?P<message>.*)group 4: the rest of the line.
Python writes a named group (?P<name>...); PowerShell and .NET write (?<name>...). Of three invented log lines (an ERROR, an INFO and a WARN), the pattern matches the ERROR and the WARN line.
Method 1 (free): Python's re.DEBUG and verbose patterns
Python's re module has a DEBUG flag that displays debug information about the compiled pattern. We ran it on 30 November 2026 (Python 3.11.9, Windows 11):
import re
re.compile(r"^(?=.*[A-Z])(?=.*\d)[A-Za-z\d!@#$%&*?]{8,}$", re.DEBUG | re.MULTILINE)
AT AT_BEGINNING
ASSERT 1
MAX_REPEAT 0 MAXREPEAT
ANY None
IN
RANGE (65, 90)
ASSERT 1
MAX_REPEAT 0 MAXREPEAT
ANY None
IN
CATEGORY CATEGORY_DIGIT
MAX_REPEAT 8 MAXREPEAT
IN
RANGE (65, 90)
RANGE (97, 122)
CATEGORY CATEGORY_DIGIT
LITERAL 33
...
The output went on to 52 lines, the last 26 being the compiled program. Every part is there, but as engine terms: ASSERT 1 is a lookahead, RANGE (65, 90) is A to Z as character codes, LITERAL 33 is the exclamation mark. To leave an explanation in the pattern itself, the same documentation describes the VERBOSE flag: spaces are ignored and # starts a comment, except inside a character class such as the one in this pattern, where # is a plain character (Sunflower#7 matched). This version matched the same two lines as the one-line pattern:
rule = re.compile(r"""
^ # start of the line
(?=.*[A-Z]) # somewhere ahead: a capital letter (checked, not matched)
(?=.*\d) # somewhere ahead: a digit
[A-Za-z\d!@#$%&*?]{8,} # 8 or more letters, digits or ! @ # $ % & * ?
$ # end of the line
""", re.MULTILINE | re.VERBOSE)
Method 2 (free): PowerShell's -match and $Matches
Windows PowerShell 5.1 uses the .NET regular expression engine. After a successful -match, the $Matches table holds the whole match under 0 and each group under its number or name. Single quotes keep PowerShell from reading the $ in the pattern:
$log = '^(?<date>\d{4}-\d{2}-\d{2}) (?<level>ERROR|WARN)\s+\[(?<module>[\w.]+)\] (?<message>.*)$'
'2026-11-30 WARN [billing.reminders] Reminder queue is 12 minutes behind' -match $log
$Matches['level'], $Matches['module'], $Matches['message']
True
WARN
billing.reminders
Reminder queue is 12 minutes behind
One difference from Python: -match ignores case by default. A line with a lower-case "warn" matched with -match and did not with -cmatch, the case-sensitive form. With the password rule it matters more: sunflower77, which has no capital letter, matched with -match and did not with -cmatch, so use -cmatch when case is part of the rule. $Matches shows what each group captured, not what each part means.
Two traps we measured
- \d is not only 0 to 9. In Python and in PowerShell, \d matched the Arabic-Indic digit three (U+0663); [0-9] did not, nor did \d with Python's re.ASCII flag or .NET's ECMAScript option. The password rule above accepts "Sunflower" followed by that digit and an x. Microsoft's page on character classes defines \d as any decimal digit in Unicode. Write [0-9] when you mean 0 to 9.
- The end anchor $ and Windows line ends. On a line that ends with \r\n, searched with the line ends kept, the message group captured "Invoice 1042 could not be sent\r", with the carriage return inside. With
(?P<message>.*?)\r?$it did not. In Python, reading the file with Path.read_text() removed the \r before the search; open() with newline='' kept it.
Online explainers such as regex101 explain a pattern next to a tester and let you choose among several flavours; Flavio Copes's regex tester uses JavaScript's RegExp, says the pattern and text never leave the page and calls itself a helper, not a full parser. We did not run them. Pick the flavour your program uses, since \d, named groups and lookarounds differ between engines, and use invented test lines rather than real log lines with customer data.
Method 3: every part in words with Regex Workbench
Regex Workbench is our Windows app for testing a pattern on sample text, with matches highlighted, group details and a plain-language explanation of each part, before a find and replace across a folder with a preview of every change, offline. It uses Python's re, so its explanations follow Python's rules. The screenshots below come from version 1.0.0 (evaluation build), with the same two patterns and similar invented test lines.
- Type the test text and the pattern, with ^ and $ at every line ticked. The password rule found 2 matches, Sunflower7 and SUNFLOWER7!, with "0 groups".
- Read the parts. Each part of the pattern has its line in words: the lookahead reads "followed by this (checked, not part of the match)", \d reads "a digit (0-9, and digits of other scripts)" and ^ reads "the start of a line".
- Check the groups. For the log pattern, the app said "4 groups: 1 “date”, 2 “level”, 3 “module”, 4 “message”", found 2 matches of the 3 lines, read the level group as "group 2, named “level” (captured)" and the alternatives as "one of 2 alternatives", and showed what each group captured for the selected match.
- Read the warnings. For both patterns it warned that $ matches just before a line feed, that in a file with Windows line ends the \r stays inside the line, and that \r?$ means the end of a line in any file. After a greedy group that is not enough:
(?P<message>.*)\r?$still captured the \r, because .* takes it first; the lazy .*? in the trap above did not.


Once a pattern does what you meant, our guide to find and replace with a regex across a folder covers running it on real files, with a preview and backups.
Limitations
- Explanations follow one engine's rules, Python's re for Regex Workbench. The same pattern can behave differently in JavaScript, .NET or Python: \d with .NET's ECMAScript option and the named-group syntax are two examples above.
- re.DEBUG output is meant for debugging and its format is not described in Python's documentation; ours comes from Python 3.11.9.
- We tested two patterns on a handful of invented lines, and did not run online explainers.
- Regex Workbench's planned requirements: Windows 10 (22H2) or 11, 64-bit.
References
- Python documentation: re, Regular expression operations
- Microsoft Learn: about_Regular_Expressions
- Microsoft Learn: Character Classes in .NET Regular Expressions
- regex101: regex101
- Flavio Copes: Regex tester & explainer