/regex rosetta stone/i

Emacs

emacs --batch, string-match
About
Emacs Lisp regexps, matched against a string with the standard syntax table and case-fold-search off. In Lisp source every backslash is doubled; the spellings here are what the engine receives.
Verified on
GNU Emacs 30.2
Offsets are counted in code points.
Found in
emacs
its own documentation
Flags
i ignore case
Character classes 6 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
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 Emacs

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 Emacs

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 Emacs

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

who has it
[[:digit:]]not[^[:digit:]] Digit

ASCII digits only: 0 to 9.

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

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

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

Also matches the no-break space.

Does not match a vertical tab.

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

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 Emacs

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 Emacs

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 Emacs

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

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

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

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

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

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

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

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

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 Emacs

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 Emacs

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 Emacs

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

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

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 Emacs

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 Emacs

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 Emacs

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 Emacs

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 Emacs

Tried: \C does not compile.

who has it
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.

Matches at the start of every line, not only at the start of the text.

test
$ End of the text or line

Also matches before a newline that ends the text.

Matches at the end of every line, not only at the end of the text.

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

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
\< Start of a word test
\> 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 Emacs

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 Emacs

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

who has it
\_< Start of symbolBefore a run of word and symbol characters, such as an identifier with an underscore in it. test
\_> End of symbol test
\= Cursor positionWhere the editor's cursor is in the buffer being searched.

Matches the empty string at point; not defined when matching against a string. Written "\\=" in Lisp source, or point in rx.

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

Quantifiers 8 of 9
SyntaxMatchesNotesTry
X?X\{0,1\} Zero or one test
X* Zero or more test
X+X\{1,\} One or more test
X\{N\} Exactly N times test
X\{N,\} N or more times test
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
n/a Possessive quantifierRepeat as many times as possible and never give any of it back. not in Emacs

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

who has it
Groups and alternation 5 of 16
SyntaxMatchesNotesTry
\(X\) Capture group test
\(?:X\) Non-capturing group test
n/a Named capture group not in Emacs

Tried: (?<n>[0-9]+) compiles, and matches as plain text or not at all.

who has it
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
n/a Backreference by name not in Emacs

Tried: (?<n>a)\k<n> compiles, and matches as plain text or not at all.

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

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 Emacs

Tried: (?>a+)b compiles, and matches as plain text or not at all.

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

Tried: (?|(a)|(b)) compiles, and matches as plain text or not at all.

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

Tried: (<)?a(?(1)>|!) compiles, and matches as plain text or not at all.

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

Tried: \((?:a|(?R))\) compiles, and matches as plain text or not at all.

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 Emacs

Tried: (a|b)(?1) compiles, and matches as plain text or not at all.

who has it
n/a Comment not in Emacs

Tried: a(?#note)b compiles, and matches as plain text or not at all.

who has it
\(?N:X\) Explicitly numbered groupA capture group that is given its number, whatever its position. test
n/a Call an earlier group by relative positionMatch the pattern of the Nth group before this point again. not in Emacs

Tried: (a|b)(?-1) compiles, and matches as plain text or not at all.

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 Emacs

Tried: (?+1)(a|b) compiles, and matches as plain text or not at all.

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

Tried: a(?=c) compiles, and matches as plain text or not at all.

who has it
n/a Negative lookahead not in Emacs

Tried: a(?!b) compiles, and matches as plain text or not at all.

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

Tried: (?<=c)a compiles, and matches as plain text or not at all.

who has it
n/a Negative lookbehind not in Emacs

Tried: (?<!b)a compiles, and matches as plain text or not at all.

who has it
Escape sequences 2 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 Emacs

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

who has it
n/a Tab not in Emacs

Tried: \t compiles, and means something else.

who has it
n/a Newline not in Emacs

Tried: \n compiles, and means something else.

who has it
n/a Carriage return not in Emacs

Tried: \r compiles, and means something else.

who has it
n/a Form feed not in Emacs

Tried: \f compiles, and means something else.

who has it
n/a Vertical tab not in Emacs

Tried: \v compiles, and means something else.

who has it
n/a Alert (bell) not in Emacs

Tried: \a compiles, and means something else.

who has it
n/a Backspace not in Emacs

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

who has it
n/a Escape character not in Emacs

Tried: \e compiles, and means something else.

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

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

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

Tried: \101 does not compile.

who has it
n/a Character by two-digit hex code not in Emacs

Tried: \x41 compiles, and means something else.

who has it
n/a Character by Unicode code point not in Emacs

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

who has it
n/a Character by Unicode name not in Emacs

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

who has it
\0\000\x00 NUL characterThe character with code 0. partial

These are Lisp string escapes that put a literal NUL in the pattern, which Emacs allows in patterns and subjects; the regexp syntax itself has no NUL escape. If an octal or hex digit follows, end the escape with a backslash and a space.

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

Flags and modes 1 of 14
SyntaxMatchesNotesTry
flag i Ignore case test
n/a MultilineThe line anchors also match at the start and end of every line. not in Emacs

Tried: (?m)^b compiles, and matches as plain text or not at all.

who has it
n/a Dot matches newline not in Emacs

Tried: (?s)a.b compiles, and matches as plain text or not at all.

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

Tried: (?x)a b compiles, and matches as plain text or not at all.

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

Tried: (?U)a.*c compiles, and matches as plain text or not at all.

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 Emacs

Tried: (?i)ab compiles, and matches as plain text or not at all.

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 Emacs

Tried: a(?i)b compiles, and matches as plain text or not at all.

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

Tried: (?i:a)b compiles, and matches as plain text or not at all.

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

Tried: (?i)a(?-i)b compiles, and matches as plain text or not at all.

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

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

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

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

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

Tried: (?n)(a)(?<x>b) compiles, and matches as plain text or not at all.

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

No locale mode. Classes follow Unicode properties and the buffer case table and syntax table, and a range is not affected by the locale collation sequence.

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 Emacs

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.

15 names: alnum alpha ascii blank cntrl digit graph lower print punct space upper xdigit nonascii multibyte

Not here: word

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 Emacs

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 Emacs

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 Emacs

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

who has it
n/a Unicode script not in Emacs

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

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

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 Emacs

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

who has it
Syntax table classes 2 of 2
SyntaxMatchesNotesTry
\sCODEnot\SCODE Syntax classA character with the given role in the syntax table: whitespace, word, symbol, punctuation, a bracket.

Follows the syntax table in effect; these results are for the standard one. A space may be written for the - code.

The codes for comments, such as <, have no members in the standard syntax table: a mode that has comments defines them.

\s with a space is the whitespace class too.

8 names: - w _ . ( ) " \

test
\cCATnot\CCAT Character categoryA character in one of the editor's character categories, most of which are scripts.

10 names: a l g y b C H K h t

test
Host language 8 of 15
SyntaxMatchesNotesTry
"PATTERN"(rx 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 and no raw string: the pattern is an ordinary Lisp string, so every backslash of the regexp is doubled (\\( for \(, "\\\\" to match one backslash) and " is written \". rx is the structured alternative, where PATTERN is a sequence of S-expression forms.

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

(let ((case-fold-search nil)) (string-match PATTERN TEXT)) Flags outside the patternWhere flags such as case-insensitive go when they are not written inside the pattern.

Case is the only flag and it is the variable case-fold-search, usually bound with let. Searches ignore case by default.

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

n/a Compile once and reuseBuild a pattern object and keep it. not in Emacs

The regexp functions expect a string; there is no compiled pattern object in Lisp. An rx form is converted to that string, typically during byte-compilation.

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

who has it
(string-match PATTERN TEXT)(string-match-p PATTERN TEXT)(re-search-forward PATTERN nil t)(looking-at PATTERN) Search a textTest whether the pattern matches anywhere in a text, or get the first match.

string-match returns the index of the first match in a string and re-search-forward searches the buffer from point; neither anchors. looking-at is anchored at point.

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

(while (re-search-forward PATTERN nil t) (match-string 0))(while (string-match PATTERN TEXT start) (setq start (match-end 0))) Every matchGet all the matches that do not overlap. partial

No function returns all matches; the manual gives an explicit loop over re-search-forward. For a string, loop over string-match with its START argument and collect (match-string 0 TEXT) in the body.

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

(replace-regexp-in-string PATTERN REPLACEMENT TEXT)(replace-regexp-in-region PATTERN REPLACEMENT)(replace-match REPLACEMENT) Replace matches

replace-regexp-in-string and replace-regexp-in-region replace every match. replace-match replaces only the text matched by the last search, so one search followed by replace-match replaces the first match (pass TEXT as its fourth argument after string-match).

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

(match-string NUM TEXT)(match-string NUM)(match-beginning NUM) Captured group, in codeRead what a group captured after a match. partial

By number only; Emacs regexps have no named groups (\(?NUM: ... \) assigns an explicit number). Pass TEXT after string-match, omit it after a buffer search.

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

\NUM Captured group, in replacement text partial

By number only and NUM is a single digit, so a digit after it is literal text; there are no named groups. In Lisp source the backslash is doubled: "\\1".

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

\& Whole match, in replacement text

The reference character is the backslash; \\ stands for one literal backslash and a bare & is literal. In Lisp source these are "\\&" and "\\\\".

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 Emacs

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 Emacs

A string has no stored search position; string-match takes an explicit START index. In a buffer the position is point: a failed re-search-forward with NOERROR t leaves it unchanged, any other non-nil NOERROR moves it to the limit.

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 Emacs

Every successful search records the match data, with no flag to ask for it. The option runs the other way: string-match-p and looking-at-p skip recording it.

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 Emacs

Patterns are plain strings with no interpolation, so there is nothing to freeze.

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 Emacs

No regexp construct runs Lisp during a match. The rx form (eval EXPR) evaluates when the pattern is built (macro-expansion time), not when the match reaches it.

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 Emacs

The rx form (regexp EXPR) inserts the regexp that EXPR evaluates to, but once, when the pattern string is built, not during the match.

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