/regex rosetta stone/i

Python

re module
About
Backtracking engine from the standard library. The third-party regex module is a different dialect.
Verified on
Python 3.15.2
Offsets are counted in code points.
Found in
python re
its own documentation
Flags
a asciii ignore casem multilines dotallx verbose
Character classes 9 of 26
SyntaxMatchesNotesTry
. Any characterAny single character. Whether that includes a newline depends on the dialect and its flags.

Does not match a newline.

test
[abc]not[^abc] One of a set of characters test
[a-z] One of a range of characters test
[\]] Backslash escape inside a setInside a bracket expression a backslash takes the special meaning from the next character. Where this is missing, a backslash there is a member like any other. test
[\d] Class escape inside a setA class escape such as the one for a digit, used as a member of a bracket expression. test
[\D] Negated class escape inside a setA negated class escape, such as the one for anything but a digit, used as a member of a bracket expression. test
\dnot\D Digit

Matches the decimal digits of every script, not only 0 to 9.

test
\wnot\W Word characterA letter, a digit or an underscore.

Unicode-aware: matches letters and digits of every script.

test
\snot\S Whitespace

Also matches the no-break space.

test
n/a Horizontal whitespaceA space or a tab, but not a line break. not in Python

Tried: \h does not compile.

who has it
n/a Vertical whitespaceA line break character of any kind. not in Python

Tried: \v compiles, and matches as plain text or not at all.

who has it
n/a Any character except newlineNever matches a newline, whatever the flags. not in Python

Tried: \N does not compile.

who has it
n/a Any line break sequenceOne line break, with CR LF taken as a single unit. not in Python

Tried: \R does not compile.

who has it
n/a Alphabetic character (shorthand) not in Python

Tried: \a compiles, and matches as plain text or not at all.

who has it
n/a Lowercase letter (shorthand) not in Python

Tried: \l does not compile.

who has it
n/a Uppercase letter (shorthand) not in Python

Tried: \u does not compile.

who has it
n/a Hexadecimal digit (shorthand) not in Python

Tried: \h does not compile.

who has it
n/a Class intersectionCharacters that are in both classes. not in Python

Tried: [a-z&&[^a-m]] compiles, and matches as plain text or not at all.

who has it
n/a Class subtractionCharacters in the first class that are not in the second. not in Python

Tried: [a-z&&[^aeiou]] compiles, and matches as plain text or not at all.

who has it
n/a Head-of-word character (shorthand)A letter or the underscore: what a word may start with. not in Python

Tried: \h does not compile.

who has it
n/a Octal digit (shorthand) not in Python

Tried: \o does not compile.

who has it
n/a Identifier characterA character allowed in an identifier. In Vim the set comes from the 'isident' option. not in Python

Tried: \i does not compile.

who has it
n/a Keyword characterA character allowed in a keyword. In Vim the set comes from the 'iskeyword' option. not in Python

Tried: \k does not compile.

who has it
n/a File name characterA character allowed in a file name. In Vim the set comes from the 'isfname' option. not in Python

Tried: \f compiles, and matches as plain text or not at all.

who has it
n/a Printable characterA character that is displayed as itself. In Vim the set comes from the 'isprint' option. not in Python

Tried: \p does not compile.

who has it
n/a Any single byteOne code unit, even where that is part of a longer character, and even a newline. not in Python

Tried: \C does not compile.

who has it
Anchors 6 of 14
SyntaxMatchesNotesTry
^ Start of the text or lineStart of the text. With a multiline flag, or always in some dialects, also the start of each line. test
$ End of the text or line

Also matches before a newline that ends the text.

test
\A Start of the textStart of the text only, whatever the flags. test
\z\Z End of the textThe very end of the text, whatever the flags. test
n/a End of the text, or before a final newline not in Python

Tried: a\Z compiles, and matches as plain text or not at all.

who has it
\b Word boundaryThe position between a word character and a non-word character or an end of the text.

Unicode-aware: a letter outside ASCII is a word character.

test
\B Not a word boundary test
n/a Start of a word not in Python

Tried: \<foo compiles, and matches as plain text or not at all.

who has it
n/a End of a word not in Python

Tried: foo\> compiles, and matches as plain text or not at all.

who has it
n/a Where the previous match endedThe position where the last match stopped, or the start of the text for the first match. not in Python

Tried: \Ga does not compile.

who has it
n/a Reset the start of the matchEverything matched so far is required but left out of the reported match. not in Python

Tried: foo\Kbar does not compile.

who has it
n/a Start of symbolBefore a run of word and symbol characters, such as an identifier with an underscore in it. not in Python

Tried: \_<b compiles, and matches as plain text or not at all.

who has it
n/a End of symbol not in Python

Tried: a\_> compiles, and matches as plain text or not at all.

who has it
n/a Cursor positionWhere the editor's cursor is in the buffer being searched. not in Python

doc Not run on an engine: taken from the documentation.

who has it
Quantifiers 9 of 9
SyntaxMatchesNotesTry
X? Zero or one test
X* Zero or more test
X+ One or more test
X{N} Exactly N times test
X{N,} N or more times test
X{,M}X{0,M} At most M times test
X{N,M} Between N and M times test
X*?X+?X?? Lazy quantifierRepeat as few times as possible instead of as many as possible. test
X*+X++X?+ Possessive quantifierRepeat as many times as possible and never give any of it back. test
Groups and alternation 9 of 16
SyntaxMatchesNotesTry
(X) Capture group test
(?:X) Non-capturing group test
(?P<NAME>X) Named capture group test
X|Y Alternation

Takes the first alternative that matches, even when a later one is longer.

test
\N Backreference by numberThe same text that capture group N matched. test
(?P=NAME) Backreference by name test
n/a Backreference by relative positionThe group N groups back from here. not in Python

Tried: (a)\g-1 does not compile.

who has it
(?>X) Atomic groupOnce the group has matched, the engine never tries a different way to match it. test
n/a Branch reset groupEvery alternative numbers its capture groups from the same start. not in Python

Tried: (?|(a)|(b)) does not compile.

who has it
(?(N)X|Y) ConditionalMatch one pattern if a capture group took part, otherwise another. test
n/a Recurse into the whole pattern not in Python

Tried: \((?:a|(?R))\) does not compile.

who has it
n/a Call a group as a subroutineMatch the pattern of capture group N again at this point, not the text it matched. not in Python

Tried: (a|b)(?1) does not compile.

who has it
(?#TEXT) Comment test
n/a Explicitly numbered groupA capture group that is given its number, whatever its position. not in Python

Tried: (?<2>a) does not compile.

who has it
n/a Call an earlier group by relative positionMatch the pattern of the Nth group before this point again. not in Python

Tried: (a|b)(?-1) does not compile.

who has it
n/a Call a later group by relative positionMatch the pattern of the Nth group after this point here as well. not in Python

Tried: (?+1)(a|b) does not compile.

who has it
Lookaround 4 of 4
SyntaxMatchesNotesTry
(?=X) LookaheadWhat follows must match X, but is not part of the match. test
(?!X) Negative lookahead test
(?<=X) LookbehindWhat comes before must match X, but is not part of the match. test
(?<!X) Negative lookbehind test
Escape sequences 13 of 16
SyntaxMatchesNotesTry
\. Escape a special characterTake the next character literally. test
n/a Quote a run of textTake everything between the markers literally. not in Python

Tried: \Qa.b\E does not compile.

who has it
\t Tab test
\n Newline test
\r Carriage return test
\f Form feed test
\v Vertical tab test
\a Alert (bell) test
[\b] Backspace test
n/a Escape character not in Python

Tried: \e does not compile.

who has it
n/a Control characterThe control character typed as Ctrl plus the letter. not in Python

Tried: \cA does not compile.

who has it
\OOO Character by octal code test
\xHH Character by two-digit hex code test
\uHHHH\UHHHHHHHH Character by Unicode code point test
\N{CHARNAME} Character by Unicode name test
\0\x00\000 NUL characterThe character with code 0.

An escape whose first digit is 0 is octal and takes up to three octal digits, so octal digits after \0 are read as part of it; \000 and \x00 are fixed width. The page states no restriction on NUL in a pattern or a subject.

doc Not run on an engine: taken from the documentation.

Flags and modes 9 of 14
SyntaxMatchesNotesTry
flag i(?i) Ignore case test
flag m(?m) MultilineThe line anchors also match at the start and end of every line. test
flag s(?s) Dot matches newline test
flag x(?x) Free spacingWhitespace in the pattern is ignored, and a # starts a comment. test
n/a Swap greedy and lazy not in Python

Tried: (?U)a.*c does not compile.

who has it
(?FLAGS) Turn a mode on inside the patternApplies from this point to the end of the pattern or the enclosing group. test
n/a Turn a mode on in the middle of the patternThe mode applies from where the flag stands, not to what comes before it. Some dialects accept an inline flag only at the very start of the pattern. not in Python

Tried: a(?i)b does not compile.

who has it
(?FLAGS:X) Turn a mode on for one group test
n/a Turn a mode off inside the pattern not in Python

Tried: (?i)a(?-i)b does not compile.

who has it
(?a) ASCII-only classesMakes the shorthand classes such as the digit class match ASCII characters only. test
(?u) Unicode classesMakes the shorthand classes such as the digit class match by Unicode property. test
n/a No automatic capturePlain parentheses group without capturing; only named groups capture. not in Python

Tried: (?n)(a)(?<x>b) does not compile.

who has it
re.Lre.LOCALE(?L)(?L:PATTERN) Locale-dependent classesClasses and case folding follow the current locale.

Usable only with bytes patterns and not together with re.ASCII; the documentation discourages it. It changes \w, \W, \b, \B and case-insensitive matching.

doc Not run on an engine: taken from the documentation.

n/a Native character set rulesPerl's rules from before 5.14: the pattern and the text decide between native and Unicode rules. not in Python

doc Not run on an engine: taken from the documentation.

who has it
POSIX named classes 0 of 1
SyntaxMatchesNotesTry
n/a POSIX named classA named set of characters, written inside a bracket expression. not in Python

Tried: [[:alnum:]]+ compiles, and matches as plain text or not at all.

who has it
POSIX collating symbols 0 of 2
SyntaxMatchesNotesTry
n/a Equivalence classEvery character that collates the same as X in the current locale. not in Python

Tried: [[=a=]] compiles, and matches as plain text or not at all.

who has it
n/a Collating symbol by nameA single collating element, written by its POSIX name inside a bracket expression. not in Python

Tried: [[.SOH.]] compiles, and matches as plain text or not at all.

who has it
Unicode properties 0 of 4
SyntaxMatchesNotesTry
n/a Unicode general category not in Python

Tried: \p{L} does not compile.

who has it
n/a Unicode script not in Python

Tried: \p{Latin} does not compile.

who has it
n/a Extended grapheme clusterOne character as a reader sees it: a base character with its combining marks. not in Python

Tried: \X does not compile.

who has it
n/a Unicode text boundaryA boundary as Unicode's segmentation rules define it, which is not the same as a boundary between word and non-word characters. not in Python

Tried: e\b{gcb} compiles, and matches as plain text or not at all.

who has it
Syntax table classes 0 of 2
SyntaxMatchesNotesTry
n/a Syntax classA character with the given role in the syntax table: whitespace, word, symbol, punctuation, a bracket. not in Python

Tried: \s- compiles, and matches as plain text or not at all.

who has it
n/a Character categoryA character in one of the editor's character categories, most of which are scripts. not in Python

Tried: \ca does not compile.

who has it
Host language 9 of 15
SyntaxMatchesNotesTry
r"PATTERN""PATTERN" Pattern in source codeHow a pattern is written where it is used: a regex literal with its delimiters, or the kind of string the documentation recommends.

No regex literal; the documentation recommends raw strings. Without the r prefix every backslash must be doubled, and even a raw literal cannot end in a single backslash.

doc Not run on an engine: taken from the documentation.

re.compile(PATTERN, FLAGS)re.search(PATTERN, TEXT, FLAGS)re.sub(PATTERN, REPLACEMENT, TEXT, flags=FLAGS) Flags outside the patternWhere flags such as case-insensitive go when they are not written inside the pattern.

FLAGS is one or more re constants joined with |, such as re.I | re.M. Passing flags positionally to re.sub and re.split is deprecated since 3.13.

doc Not run on an engine: taken from the documentation.

re.compile(PATTERN) Compile once and reuseBuild a pattern object and keep it.

Returns a re.Pattern with search, findall, sub and the other methods. The module-level functions also cache the most recently used patterns.

doc Not run on an engine: taken from the documentation.

re.search(PATTERN, TEXT) Search a textTest whether the pattern matches anywhere in a text, or get the first match.

re.match matches only at the start of the string; 3.15 adds the name re.prefixmatch for it and soft-deprecates match. re.fullmatch requires the whole string to match.

doc Not run on an engine: taken from the documentation.

re.findall(PATTERN, TEXT)re.finditer(PATTERN, TEXT) Every matchGet all the matches that do not overlap.

findall returns strings, or tuples of groups when the pattern has more than one group; finditer yields Match objects.

doc Not run on an engine: taken from the documentation.

re.sub(PATTERN, REPLACEMENT, TEXT) Replace matches

Replaces every match; pass count=1 for the first only.

doc Not run on an engine: taken from the documentation.

m.group(NUM)m.group("NAME")m[NUM]m["NAME"] Captured group, in codeRead what a group captured after a match.

m is the Match object returned by a search. Group 0 is the whole match.

doc Not run on an engine: taken from the documentation.

\NUM\g<NUM>\g<NAME> Captured group, in replacement text

\g<NUM> keeps a following digit apart: \g<2>0 is group 2 then 0, while \20 is group 20.

doc Not run on an engine: taken from the documentation.

\g<0> Whole match, in replacement text

A literal backslash in the replacement is written \\.

doc Not run on an engine: taken from the documentation.

n/a Match once until resetA match operator that succeeds one time only, until it is reset. not in Python

doc Not run on an engine: taken from the documentation.

who has it
n/a Keep the position after a failed matchIn a repeated search, a failed match does not send the next one back to the start. not in Python

A pattern object stores no search position; Pattern.search(TEXT, pos) takes the start index as an argument each time.

doc Not run on an engine: taken from the documentation.

who has it
n/a Keep the matched text for laterAsk for the matched text, and the text around it, to be saved. not in Python

No flag is needed or offered: every Match object carries m.string, m.start() and m.end().

doc Not run on an engine: taken from the documentation.

who has it
n/a Interpolate and compile only onceA pattern literal with variables in it is built the first time and never again. not in Python

Patterns are ordinary strings, so there is no interpolating literal to freeze; calling re.compile once and keeping the object is the documented way to build a pattern one time.

doc Not run on an engine: taken from the documentation.

who has it
n/a Code inside a patternHost-language code that runs when the match reaches it. not in Python

doc Not run on an engine: taken from the documentation.

who has it
n/a Pattern computed during the matchCode inside the pattern whose result is matched as a pattern at that point. not in Python

doc Not run on an engine: taken from the documentation.

who has it

Every row was run on the engine named above, except the rows marked doc: those describe something no regex engine can run by itself, such as the syntax of the host language, and are taken from the documentation they link to. For a row marked "not in Python", the spelling other dialects use was tried and the line starting "Tried" says what it did here; that shows the spelling does not work, not that no other spelling exists.