/regex rosetta stone/i

POSIX ERE

regcomp, extended syntax
About
POSIX.1-2024 extended regular expressions. The baseline is the standard's text; a spelling beyond it is tagged with the C libraries that accept it.
Verified on
glibc 2.41, musl 1.2.6
Offsets are counted in bytes.
Found in
grep -E, sed -E, awk, bash [[ =~ ]], C regcomp()
its own documentation
Flags
i ignore casen newline
Character classes 6 of 26
SyntaxMatchesNotesTry
. Any characterAny single character. Whether that includes a newline depends on the dialect and its flags.

Matches a newline.

test
[abc]not[^abc] One of a set of characters test
[a-z] One of a range of characters test
n/a 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. not in POSIX ERE

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

who has it
n/a Class escape inside a setA class escape such as the one for a digit, used as a member of a bracket expression. not in POSIX ERE

Tried: [\d] compiles, and means something else.

who has it
n/a 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. not in POSIX ERE

Tried: [\D] compiles, and means something else.

who has it
[[:digit:]]\dmuslnot[^[:digit:]] Digit

ASCII digits only: 0 to 9.

test
[[:alnum:]_]\wgnumuslnot[^[:alnum:]_] Word characterA letter, a digit or an underscore.

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

test
[[:space:]]\sgnumuslnot[^[:space:]] Whitespace

Does not match the no-break space.

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

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

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

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 POSIX ERE

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

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

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

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

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

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

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

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

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

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

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

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

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 POSIX ERE

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 POSIX ERE

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

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

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

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

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

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

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

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 POSIX ERE

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 POSIX ERE

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

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 POSIX ERE

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

who has it
Anchors 8 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

Matches only at the very end, not before a final newline.

test
\`gnu Start of the textStart of the text only, whatever the flags. test
\'gnu 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 POSIX ERE

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

who has it
\bgnumusl 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
\Bgnumusl Not a word boundary test
\<gnumusl Start of a word test
\>gnumusl End of a word test
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 POSIX ERE

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

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 POSIX ERE

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

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 POSIX ERE

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

who has it
n/a End of symbol not in POSIX ERE

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 POSIX ERE

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

who has it
Quantifiers 7 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{0,M}X{,M}gnu At most M times test
X{N,M} Between N and M times test
n/a Lazy quantifierRepeat as few times as possible instead of as many as possible. not in POSIX ERE

Tried: a.*?c compiles, and means something else.

who has it
n/a Possessive quantifierRepeat as many times as possible and never give any of it back. not in POSIX ERE

Tried: a*+a compiles, and means something else.

who has it
Groups and alternation 3 of 16
SyntaxMatchesNotesTry
(X) Capture group test
n/a Non-capturing group not in POSIX ERE

Tried: (?:ab)(c) does not compile.

who has it
n/a Named capture group not in POSIX ERE

Tried: (?<n>[0-9]+) does not compile.

who has it
X|Y Alternation

Takes the longest match among the alternatives (leftmost-longest).

test
\Ngnu Backreference by numberThe same text that capture group N matched. test
n/a Backreference by name not in POSIX ERE

Tried: (?<n>a)\k<n> does not compile.

who has it
n/a Backreference by relative positionThe group N groups back from here. not in POSIX ERE

Tried: (a)\g-1 compiles, and matches as plain text or not at all.

who has it
n/a Atomic groupOnce the group has matched, the engine never tries a different way to match it. not in POSIX ERE

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

who has it
n/a Branch reset groupEvery alternative numbers its capture groups from the same start. not in POSIX ERE

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

who has it
n/a ConditionalMatch one pattern if a capture group took part, otherwise another. not in POSIX ERE

Tried: (<)?a(?(1)>|!) does not compile.

who has it
n/a Recurse into the whole pattern not in POSIX ERE

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 POSIX ERE

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

who has it
n/a Comment not in POSIX ERE

Tried: a(?#note)b does not compile.

who has it
n/a Explicitly numbered groupA capture group that is given its number, whatever its position. not in POSIX ERE

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 POSIX ERE

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 POSIX ERE

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

who has it
Lookaround 0 of 4
SyntaxMatchesNotesTry
n/a LookaheadWhat follows must match X, but is not part of the match. not in POSIX ERE

Tried: a(?=c) does not compile.

who has it
n/a Negative lookahead not in POSIX ERE

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

who has it
n/a LookbehindWhat comes before must match X, but is not part of the match. not in POSIX ERE

Tried: (?<=c)a does not compile.

who has it
n/a Negative lookbehind not in POSIX ERE

Tried: (?<!b)a does not compile.

who has it
Escape sequences 9 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 POSIX ERE

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

who has it
\tmusl Tab test
\nmusl Newline test
\rmusl Carriage return test
\fmusl Form feed test
n/a Vertical tab not in POSIX ERE

Tried: \v compiles, and means something else.

who has it
\amusl Alert (bell) test
n/a Backspace not in POSIX ERE

Tried: [\b] compiles, and means something else.

who has it
\emusl Escape character test
n/a Control characterThe control character typed as Ctrl plus the letter. not in POSIX ERE

Tried: \cA compiles, and means something else.

who has it
n/a Character by octal code not in POSIX ERE

Tried: \o{101} compiles, and matches as plain text or not at all.

who has it
\xHHmusl Character by two-digit hex code test
\x{HHHH}musl Character by Unicode code point test
n/a Character by Unicode name not in POSIX ERE

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

who has it
n/a NUL characterThe character with code 0. not in POSIX ERE

The POSIX interfaces do not permit a NUL in a pattern or in the string to be matched. awk adds octal escapes \ddd to EREs, but all zeros is undefined, as is any NUL in an awk pattern or text.

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

who has it
Flags and modes 2 of 14
SyntaxMatchesNotesTry
flag i Ignore case test
flag n MultilineThe line anchors also match at the start and end of every line. test
n/a Dot matches newline not in POSIX ERE

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

who has it
n/a Free spacingWhitespace in the pattern is ignored, and a # starts a comment. not in POSIX ERE

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

who has it
n/a Swap greedy and lazy not in POSIX ERE

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

who has it
n/a Turn a mode on inside the patternApplies from this point to the end of the pattern or the enclosing group. not in POSIX ERE

Tried: (?i)ab does not compile.

who has it
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 POSIX ERE

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

who has it
n/a Turn a mode on for one group not in POSIX ERE

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

who has it
n/a Turn a mode off inside the pattern not in POSIX ERE

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

who has it
n/a ASCII-only classesMakes the shorthand classes such as the digit class match ASCII characters only. not in POSIX ERE

Tried: (?a)\d does not compile.

who has it
n/a Unicode classesMakes the shorthand classes such as the digit class match by Unicode property. not in POSIX ERE

Tried: (?u)\d does not compile.

who has it
n/a No automatic capturePlain parentheses group without capturing; only named groups capture. not in POSIX ERE

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

who has it
n/a Locale-dependent classesClasses and case folding follow the current locale. not in POSIX ERE

Not a switch: POSIX matching always follows the current locale, with character classes from LC_CTYPE and ranges, equivalence classes and collating elements from LC_COLLATE. Calling regexec under a locale different from the one in effect at regcomp is undefined, and ranges are unspecified outside the POSIX locale.

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

who has it
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 POSIX ERE

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.

12 names: alnum alpha blank cntrl digit graph lower print punct space upper xdigit

Not here: ascii word nonascii multibyte

test
POSIX collating symbols 2 of 2
SyntaxMatchesNotesTry
[[=X=]]gnu Equivalence classEvery character that collates the same as X in the current locale. test
[[.NAME.]]gnu Collating symbol by nameA single collating element, written by its POSIX name inside a bracket expression.

52 names: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z

Not here: SOH STX ETX EOT ENQ ACK alert backspace tab newline vertical-tab form-feed carriage-return SO SI DLE DC1 DC2 DC3 DC4 NAK SYN ETB CAN EM SUB ESC IS4 IS3 IS2 IS1 space exclamation-mark quotation-mark number-sign dollar-sign percent-sign ampersand apostrophe left-parenthesis right-parenthesis asterisk plus-sign comma hyphen period slash zero one two three four five six seven eight nine colon semicolon less-than-sign equals-sign greater-than-sign question-mark commercial-at left-square-bracket backslash right-square-bracket circumflex underscore grave-accent left-curly-bracket vertical-line right-curly-bracket tilde DEL

test
Unicode properties 0 of 4
SyntaxMatchesNotesTry
n/a Unicode general category not in POSIX ERE

Tried: \p{L} does not compile.

who has it
n/a Unicode script not in POSIX ERE

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 POSIX ERE

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

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 POSIX ERE

Tried: e\b{gcb} does not compile.

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 POSIX ERE

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 POSIX ERE

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

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

/PATTERN/ is the awk ERE token (a slash inside is written \/) and also a sed -E context address, which can use another delimiter as \cPATTERNc. "PATTERN" is the C string given to regcomp, or an awk string used as a pattern, where the awk escapes are processed a second time.

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

regcomp(&re, PATTERN, REG_EXTENDED | FLAGS)grep -E -i PATTERNs/PATTERN/REPLACEMENT/FLAGS Flags outside the patternWhere flags such as case-insensitive go when they are not written inside the pattern.

C: FLAGS is the bitwise OR of REG_ICASE, REG_MINIMAL, REG_NOSUB, REG_NEWLINE. grep has -i, the s command of sed -E takes the flag i, and awk has no matching flags.

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

regcomp(&re, PATTERN, REG_EXTENDED) Compile once and reuseBuild a pattern object and keep it.

C only: re is a regex_t reused by regexec and released with regfree(&re). grep, sed and awk have no pattern objects.

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

regexec(&re, TEXT, 0, NULL, 0)TEXT ~ /PATTERN/match(TEXT, /PATTERN/)grep -E PATTERN Search a textTest whether the pattern matches anywhere in a text, or get the first match.

None of these anchors. regexec returns 0 on a match; awk ~ gives 1 or 0 and match() gives the 1-based position or 0 and sets RSTART and RLENGTH; grep -E selects lines the pattern matches in any part.

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

regexec(&re, TEXT + pm.rm_eo, 1, &pm, REG_NOTBOL) Every matchGet all the matches that do not overlap. partial

No call returns every match: the regcomp page shows a loop that calls regexec again from the end of the previous match with REG_NOTBOL. grep selects whole lines and has no option to print each match, and awk has no such function.

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

s/PATTERN/REPLACEMENT/s/PATTERN/REPLACEMENT/gsub(/PATTERN/, REPLACEMENT, TEXT)gsub(/PATTERN/, REPLACEMENT, TEXT) Replace matches

The C interface has no substitution function. sed -E s replaces the first match in the pattern space and all with the g flag; awk sub replaces the first match and gsub all, changing the variable TEXT in place and returning the count.

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

pmatch[NUM].rm_sopmatch[NUM].rm_eo Captured group, in codeRead what a group captured after a match. partial

C only and by number only: regexec(&re, TEXT, nmatch, pmatch, 0) fills in byte offsets into TEXT, not strings, with pmatch[0] for the whole match. There are no named groups, and awk gives no access to groups.

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

\NUM Captured group, in replacement text partial

sed -E s replacement only, by number, one digit, so a digit after it is literal text. There are no named groups, and awk sub and gsub cannot refer to groups.

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

& Whole match, in replacement text

Both in the sed s replacement and in awk sub and gsub. A literal ampersand is \&, which in an awk string literal 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 POSIX ERE

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 POSIX ERE

regexec keeps no search position; the caller passes the address to start from on each call.

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 POSIX ERE

Match offsets are reported whenever nmatch is not 0. The switch runs the other way: REG_NOSUB asks for success or failure only.

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 POSIX ERE

No flag of this kind among the regcomp flags; in C the pattern is compiled exactly when regcomp is called.

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 POSIX ERE

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 POSIX ERE

Nothing inside a pattern is evaluated. In awk any expression that is not an ERE token can stand where a pattern is expected and its string value is used as the ERE, but it is evaluated before matching starts.

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 POSIX ERE", 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.