Regexdrill

Flags and tools Updated November 2026

Regex in Python (re)

Python's re module: re.search(p, s) finds the first match anywhere, re.match only at the start, re.fullmatch the whole string, re.findall returns all matches as strings (or tuples of groups), re.sub replaces, re.split splits. Write patterns as raw strings (r prefix) so backslashes survive, and use re.compile when a pattern is reused.

Syntax

re.search(pattern, string, flags=0)   re.findall(...)   re.sub(pattern, repl, string)   re.compile(pattern, flags)
PartWhat it is
re.searchfirst match anywhere; returns a Match object or None
re.matchmatch only at the start of the string
re.fullmatchthe whole string must match
re.findalllist of all matches; with groups, a list of tuples
re.subreplace matches; the replacement uses \1 or \g<name>
flagsre.I ignore case, re.M multiline, re.S dot matches newline

Examples

import re
re.search(r"\d+", "order 66").group()

Result: '66'

re.findall(r"(\w+)@(\w+)\.com", "a@x.com b@y.com")

Result: [('a', 'x'), ('b', 'y')]

With capturing groups, findall returns tuples of the groups, not the whole matches.

re.sub(r"\s+", " ", "a   b \t c")

Result: 'a b c'

re.fullmatch(r"\d{4}-\d{2}-\d{2}", "2024-03-15") is not None

Result: True

Common mistakes

  • Using re.match to search inside a string; it only matches at position 0. Use re.search.
  • Forgetting the raw string, so "\b" becomes a backspace and "\d" raises a warning; write r"\bword\b".
  • Calling findall with helper groups and getting tuples; make them non-capturing (?:...) or use finditer.
  • Using $1 in re.sub; Python needs \1 or \g<name> in the replacement string.

Practice Regex in Python (re)

These drills check your answer on live data, free, no account needed.

  • key=value pairsLines hold key=value pairs separated by semicolons. Capture every pair: the key (word characters) in group 1 and the value (everything up to the next semicolon, possibly empty) in group 2.
  • Log lines: level and messageEach log line has a level in square brackets, like [ERROR], followed by a space and a message. Capture the capitalized level in group 1 and the rest of the line in group 2.
  • Named groups: year, month, dayFind the first date written YYYY-MM-DD and capture its parts in named groups called year, month and day.

Questions

What is the difference between re.match and re.search?

re.match anchors at the start of the string (like a leading ^); re.search scans the whole string for the first match. For a whole-string check use re.fullmatch.

How do I use flags in Python regex?

Pass them as the flags argument and combine with |: re.findall(r"^error.*$", log, re.I | re.M). Inline flags like (?i) at the start of the pattern also work.

How do named groups work in Python?

Use (?P<name>...) and read m.group("name") or m.groupdict(). Python 3 re does not accept the (?<name>...) spelling used by JavaScript.

Related

  • Named groups (?<name>...)How to capture by name, read match.groups.name, use $<name> in replacements and \k<name> as a backreference, and the (?P<name>) syntax Python needs.
  • Lookbehind (?<=...) and (?<!...)How lookbehind checks what precedes the match, grabbing the number after a dollar sign or excluding negatives, browser and Python support, and width limits.
  • Regex flagsWhat each JavaScript regex flag does (case-insensitive, global, multiline, dotAll, unicode, sticky), how to set them, and the lastIndex trap with g and test.
  • match, test, replace in JavaScriptWhich JavaScript regex method to use for a yes/no check, the first match, all matches with groups, substitution and splitting, with return values and g rules.

Previous: match, test, replace in JavaScriptNext: Regex in Excel