/regex rosetta stone/i

PCRE2

libpcre2, interpreter
About
Backtracking engine, Perl-compatible. Run here in UTF mode without JIT.
Verified on
PCRE2 10.49
Offsets are counted in bytes.
Found in
PHP preg_*, grep -P, nginx, Apache httpd, HAProxy, R, git grep -P
its own documentation
Flags
U ungreedyi ignore casem multilinen no captures dotallu unicodex verbose
Character classes 16 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
\d[[:digit:]]not\D Digit

ASCII digits only: 0 to 9.

test
\w[[:alnum:]_]not\W Word characterA letter, a digit or an underscore.

ASCII only: A to Z, a to z, 0 to 9 and _.

test
\s[[:space:]]not\S Whitespace

Does not match the no-break space.

test
\hnot\H Horizontal whitespaceA space or a tab, but not a line break. test
\vnot\V Vertical whitespaceA line break character of any kind. test
\N Any character except newlineNever matches a newline, whatever the flags. test
\R Any line break sequenceOne line break, with CR LF taken as a single unit. test
n/a Alphabetic character (shorthand) not in PCRE2

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

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

Tried: \l does not compile.

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

Tried: \u does not compile.

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

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

who has it
(?[ X & Y ]) Class intersectionCharacters that are in both classes. test
(?[ X - Y ]) Class subtractionCharacters in the first class that are not in the second. test
n/a Head-of-word character (shorthand)A letter or the underscore: what a word may start with. not in PCRE2

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

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

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 PCRE2

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 PCRE2

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 PCRE2

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 PCRE2

Tried: \p does not compile.

who has it
\C Any single byteOne code unit, even where that is part of a longer character, and even a newline. test
Anchors 11 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 End of the textThe very end of the text, whatever the flags. test
\Z End of the text, or before a final newline test
\b Word boundaryThe position between a word character and a non-word character or an end of the text.

ASCII word characters only: a letter outside ASCII counts as a non-word character.

test
\B Not a word boundary test
[[:<:]] Start of a word test
[[:>:]] End of a word test
\G Where the previous match endedThe position where the last match stopped, or the start of the text for the first match. test
\K Reset the start of the matchEverything matched so far is required but left out of the reported match. test
n/a Start of symbolBefore a run of word and symbol characters, such as an identifier with an underscore in it. not in PCRE2

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

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

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 PCRE2

There is no buffer or cursor. The nearest assertion is \G, true at the startoffset passed to pcre2_match().

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 15 of 16
SyntaxMatchesNotesTry
(X) Capture group test
(?:X) Non-capturing group test
(?<NAME>X)(?'NAME'X)(?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\gN\g{N} Backreference by numberThe same text that capture group N matched. test
\k<NAME>\k'NAME'\k{NAME}\g{NAME}(?P=NAME) Backreference by name test
\g-N\g{-N} Backreference by relative positionThe group N groups back from here. test
(?>X) Atomic groupOnce the group has matched, the engine never tries a different way to match it. test
(?|X) Branch reset groupEvery alternative numbers its capture groups from the same start. test
(?(N)X|Y) ConditionalMatch one pattern if a capture group took part, otherwise another. test
(?R)\g<0> Recurse into the whole pattern test
(?N)\g<N> Call a group as a subroutineMatch the pattern of capture group N again at this point, not the text it matched. test
(?#TEXT) Comment test
n/a Explicitly numbered groupA capture group that is given its number, whatever its position. not in PCRE2

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

who has it
(?-N)\g<-N> Call an earlier group by relative positionMatch the pattern of the Nth group before this point again. test
(?+N)\g<+N> Call a later group by relative positionMatch the pattern of the Nth group after this point here as well. test
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 14 of 16
SyntaxMatchesNotesTry
\. Escape a special characterTake the next character literally. test
\QTEXT\E Quote a run of textTake everything between the markers literally. test
\t Tab test
\n Newline test
\r Carriage return test
\f Form feed test
n/a Vertical tab not in PCRE2

Tried: \v compiles, and means something else.

who has it
\a Alert (bell) test
[\b] Backspace test
\e Escape character test
\cC Control characterThe control character typed as Ctrl plus the letter. test
\OOO\o{OOO} Character by octal code test
\xHH Character by two-digit hex code test
\x{HHHH}\N{U+HHHH} Character by Unicode code point test
n/a Character by Unicode name not in PCRE2

Tried: \N{DIGIT ONE} does not compile.

who has it
\0\x00\o{0}\000 NUL characterThe character with code 0.

After \0 up to two further octal digits are read, so write \000 or \x00 before an octal digit; under PCRE2_EXTRA_NO_BS0 a bare \0 is rejected and \00 is needed. A pcre2grep pattern given on the command line cannot contain a binary zero itself, one read with -f can.

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

Flags and modes 13 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
flag U(?U) Swap greedy and lazy test
(?FLAGS) Turn a mode on inside the patternApplies from this point to the end of the pattern or the enclosing group. test
(?FLAGS) 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. test
(?FLAGS:X) Turn a mode on for one group test
(?-FLAGS) Turn a mode off inside the pattern test
(?a) ASCII-only classesMakes the shorthand classes such as the digit class match ASCII characters only. test
(*UCP) Unicode classesMakes the shorthand classes such as the digit class match by Unicode property. test
(?n) No automatic capturePlain parentheses group without capturing; only named groups capture. test
pcre2_set_character_tables(ccontext, pcre2_maketables(NULL))pcre2grep --locale=fr_FR PATTERN Locale-dependent classesClasses and case folding follow the current locale. partial

Not a pattern mode and there is no (?l): character tables built by pcre2_maketables() in the current C locale are attached to the compile context, which pcre2grep does from --locale, LC_ALL or LC_CTYPE. The tables cover only code points below 256, and the documentation discourages mixing locales with Unicode support.

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 PCRE2

The inline option letters are a, i, J, m, n, r, s, U, x and xx; there is no d. PHP adds its own modifier letters and has no d either.

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

who has it
POSIX named classes 1 of 1
SyntaxMatchesNotesTry
[[:NAME:]]not[^[:NAME:]] POSIX named classA named set of characters, written inside a bracket expression.

14 names: alnum alpha ascii blank cntrl digit graph lower print punct space upper word xdigit

Not here: nonascii multibyte

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

Tried: [[=a=]] does not compile.

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

Tried: [[.SOH.]] does not compile.

who has it
Unicode properties 3 of 4
SyntaxMatchesNotesTry
\p{NAME}not\P{NAME} Unicode general category

36 names: L Ll Lm Lo Lt Lu C Cc Cf Cn Co M Mc Me Mn N Nd Nl No P Pc Pd Pe Pi Pf Po Ps S Sc Sk Sm So Z Zl Zp Zs

test
\p{NAME}not\P{NAME} Unicode script

Goes by Script_Extensions: it also matches a mark shared between scripts, such as a combining accent.

8 names: Latin Greek Cyrillic Hebrew Arabic Han Hiragana Thai

test
\X Extended grapheme clusterOne character as a reader sees it: a base character with its combining marks. test
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 PCRE2

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 PCRE2

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 PCRE2

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

who has it
Host language 10 of 15
SyntaxMatchesNotesTry
"PATTERN""/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.

C API (first form) and pcre2grep take the bare pattern with no delimiters; the C API reads a pointer plus a length or PCRE2_ZERO_TERMINATED. PHP (other forms) requires delimiters, a delimiter inside the pattern must be backslash-escaped, and the PHP string literal consumes one level of backslashes.

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

pcre2_compile(PATTERN, PCRE2_ZERO_TERMINATED, FLAGS, &errorcode, &erroroffset, NULL)"/PATTERN/FLAGS"pcre2grep -i PATTERN Flags outside the patternWhere flags such as case-insensitive go when they are not written inside the pattern.

C API: FLAGS is an OR of option bits such as PCRE2_CASELESS, zero for none. PHP: modifier letters after the closing delimiter; pcre2grep: command-line options such as -i.

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

pcre2_compile(PATTERN, PCRE2_ZERO_TERMINATED, 0, &errorcode, &erroroffset, NULL) Compile once and reuseBuild a pattern object and keep it.

Returns a pcre2_code * that pcre2_match() accepts any number of times; release it with pcre2_code_free(). PHP preg functions and pcre2grep expose no separate compile step.

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

pcre2_match(re, TEXT, PCRE2_ZERO_TERMINATED, 0, 0, match_data, NULL)preg_match(PATTERN, TEXT)pcre2grep PATTERN Search a textTest whether the pattern matches anywhere in a text, or get the first match.

C API: re is the result of pcre2_compile() and the search is unanchored unless PCRE2_ANCHORED is set; PCRE2_ERROR_NOMATCH means no match. PHP preg_match returns 1 or 0; pcre2grep searches the lines of files or standard input.

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

pcre2_next_match(match_data, &start_offset, &options)preg_match_all(PATTERN, TEXT, $matches)pcre2grep -o PATTERN Every matchGet all the matches that do not overlap.

C API: there is no single call; loop over pcre2_match() and let pcre2_next_match() (10.47 and later) supply the next start offset and options. PHP: preg_match_all; pcre2grep: -o prints each match on its own line.

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

pcre2_substitute(re, TEXT, PCRE2_ZERO_TERMINATED, 0, 0, NULL, NULL, REPLACEMENT, PCRE2_ZERO_TERMINATED, outputbuffer, &outlength)preg_replace(PATTERN, REPLACEMENT, TEXT) Replace matches

C API: replaces the first match only; add the option PCRE2_SUBSTITUTE_GLOBAL for all. PHP preg_replace replaces all; pass 1 as the fourth argument (limit) for the first only.

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

pcre2_substring_get_bynumber(match_data, NUM, &buffer, &length)pcre2_substring_get_byname(match_data, "NAME", &buffer, &length)$matches[NUM]$matches["NAME"] Captured group, in codeRead what a group captured after a match.

First two are the C API, where group 0 is the whole match and the ovector from pcre2_get_ovector_pointer() gives the offsets directly. Last two are PHP after preg_match(PATTERN, TEXT, $matches).

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

$NUM${NUM}$NAME${NAME} Captured group, in replacement text

These are the pcre2_substitute() forms; braces are required only when the next character would read as part of the number or name, and $<NAME> is accepted too. PHP preg_replace documents $NUM, \NUM and ${NUM}, and no named form.

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

$0$& Whole match, in replacement text

pcre2_substitute(): write $$ for a literal dollar, or set PCRE2_SUBSTITUTE_LITERAL to switch interpretation off. PHP preg_replace: $0 or \0.

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 PCRE2

Every pcre2_match() call is independent; no option or function records that a pattern has already matched. PHP preg and pcre2grep have nothing comparable either.

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 PCRE2

The library stores no search position: the caller passes startoffset to every pcre2_match() call, so a failed match has nothing to reset. PHP preg_match also takes an explicit offset argument.

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 PCRE2

The match data block always holds the offsets of the whole match, so no switch is needed. The nearest option, PCRE2_COPY_MATCHED_SUBJECT, copies the subject after a successful match so extraction still works once the caller frees the original.

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 PCRE2

PCRE2 has no variables and no interpolation; a compiled pattern is reused for as long as the caller keeps the pcre2_code. PHP has no o modifier.

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

who has it
(?C)(?C1)(?C"CODE") Code inside a patternHost-language code that runs when the match reaches it. partial

A callout, not embedded code: the pattern marks a point and passes a number below 256 or a delimited string to a function the application registered with pcre2_set_callout(). pcre2grep treats the string as an external program to run, or as text to echo when it starts with a pipe character.

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

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

The documentation says (??{code}) is not supported. A callout cannot supply a pattern; it can only let matching proceed, backtrack or fail.

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 PCRE2", 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.