/regex rosetta stone/i

Vim

vim, string matching
About
Patterns are run with matchstrpos(), so ^ and $ anchor at the ends of the text and items that need a buffer have no meaning. The base column is magic, the default.
Verified on
Vim 9.2.1352
Offsets are counted in bytes.
Found in
vim, gvim
its own documentation
Flags
i ignore case
Modes
This page describes magic. very magic rows show where another mode reads a pattern differently.
Character classes 18 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
[\]] 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
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 Vim

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 Vim

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

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

Does not match a vertical tab.

The negated form \S leaves out only the space and the tab, so it matches a line break.

test
\s Horizontal whitespaceA space or a tab, but not a line break. test
n/a Vertical whitespaceA line break character of any kind. not in Vim

Tried: \v compiles, and means something else.

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

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 Vim

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

who has it
\anot\A Alphabetic character (shorthand) test
\lnot\L Lowercase letter (shorthand) test
\unot\U Uppercase letter (shorthand) test
\xnot\X Hexadecimal digit (shorthand) test
n/a Class intersectionCharacters that are in both classes. not in Vim

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 Vim

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

who has it
\hnot\H Head-of-word character (shorthand)A letter or the underscore: what a word may start with. test
\onot\O Octal digit (shorthand) test
\i Identifier characterA character allowed in an identifier. In Vim the set comes from the 'isident' option.

The set comes from the isident option.

\I is \i without the digits.

test
\k Keyword characterA character allowed in a keyword. In Vim the set comes from the 'iskeyword' option.

The set comes from the iskeyword option.

\K is \k without the digits.

test
\f File name characterA character allowed in a file name. In Vim the set comes from the 'isfname' option.

The set comes from the isfname option.

\F is \f without the digits.

test
\p Printable characterA character that is displayed as itself. In Vim the set comes from the 'isprint' option.

The set comes from the isprint option.

\P is \p without the digits.

test
n/a Any single byteOne code unit, even where that is part of a longer character, and even a newline. not in Vim

Tried: \C compiles, and means something else.

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
\%^
very magic%^
Start of the textStart of the text only, whatever the flags. test
\%$
very magic%$
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 Vim

Tried: a\Z compiles, and means something else.

who has it
n/a Word boundaryThe position between a word character and a non-word character or an end of the text. not in Vim

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

who has it
n/a Not a word boundary not in Vim

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

who has it
\<
very magic<
Start of a word test
\>
very magic>
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 Vim

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

who has it
\zs 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 Vim

Tried: \_<b does not compile.

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

Tried: a\_> does not compile.

who has it
\%# Cursor positionWhere the editor's cursor is in the buffer being searched.

Only works when matching in a buffer displayed in a window. \%.l, \%.c and \%.v match in the cursor line, column and virtual column.

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

Quantifiers 8 of 9
SyntaxMatchesNotesTry
X\=X\?X\{0,1\}
very magicX?X=
Zero or one test
X* Zero or more test
X\+X\{1,\}
very magicX+
One or more test
X\{N}X\{N\}
very magicX{N}
Exactly N times test
X\{N,}X\{N,\}
very magicX{N,}
N or more times test
X\{,M}X\{0,M\}
very magicX{,M}X{0,M}
At most M times test
X\{N,M}X\{N,M\}
very magicX{N,M}
Between N and M times test
X\{-}
very magicX{-}
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 Vim

Tried: a*+b compiles, and matches as plain text or not at all.

who has it
Groups and alternation 5 of 16
SyntaxMatchesNotesTry
\(X\)
very magic(X)
Capture group test
\%(X\)
very magic%(X)
Non-capturing group test
n/a Named capture group not in Vim

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

who has it
X\|Y
very magicX|Y
Alternation

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

test
\N
very magic\N
Backreference by numberThe same text that capture group N matched. test
n/a Backreference by name not in Vim

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 Vim

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

who has it
X\@>
very magicX@>
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 Vim

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 Vim

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 Vim

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 Vim

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

who has it
n/a Comment not in Vim

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

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

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

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

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 Vim

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

who has it
Lookaround 4 of 4
SyntaxMatchesNotesTry
X\@=
very magicX@=
LookaheadWhat follows must match X, but is not part of the match. test
X\@!
very magicX@!
Negative lookahead test
X\@<=
very magicX@<=
LookbehindWhat comes before must match X, but is not part of the match. test
X\@<!
very magicX@<!
Negative lookbehind test
Escape sequences 10 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 Vim

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

who has it
\t Tab test
\n Newline test
\r Carriage return test
n/a Form feed not in Vim

Tried: \f compiles, and means something else.

who has it
n/a Vertical tab not in Vim

Tried: \v compiles, and means something else.

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

Tried: \a compiles, and means something else.

who has it
\b[\b] Backspace test
\e Escape character test
n/a Control characterThe control character typed as Ctrl plus the letter. not in Vim

Tried: \cA compiles, and means something else.

who has it
\%oOOO[\oOOO]
very magic%oOOO
Character by octal code test
\%xHH[\xHH]
very magic%xHH
Character by two-digit hex code test
\%uHHHH\%UHHHHHHHH[\uHHHH][\UHHHHHHHH]
very magic%uHHHH%UHHHHHHHH
Character by Unicode code point test
n/a Character by Unicode name not in Vim

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

who has it
\%d0\%x00\%o0\%u0000 NUL characterThe character with code 0.

NUL exists only in buffer text (stored there as a newline character; a Vim string cannot contain it), and the manual documents typing the character with CTRL-V 000 without singling out code 0 for these escapes. \%d0 must be followed by a non-digit and \%o0 by a non-octal digit.

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

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

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

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

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 Vim

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 Vim

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 Vim

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 Vim

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 Vim

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 Vim

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 Vim

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 Vim

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 Vim

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 Vim

No locale mode. \w, \a, \l, \u and the like match fixed ASCII ranges, and the [:alpha:] style classes are documented as ASCII with a system-dependent implementation.

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 Vim

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

who has it
POSIX named classes 1 of 1
SyntaxMatchesNotesTry
[[:NAME:]]not[^[:NAME:]]
very magic[[: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=]] Equivalence classEvery character that collates the same as X in the current locale. test
[[.NAME.]] Collating symbol by nameA single collating element, written by its POSIX name inside a bracket expression.

In a set, the members written after a collating symbol are ignored: put the symbol last.

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 Vim

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

who has it
n/a Unicode script not in Vim

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 Vim

Tried: \X compiles, and means something else.

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 Vim

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 Vim

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 Vim

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

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

No regex literal: a pattern is a string in an expression or sits between delimiters in a command, and :s accepts another single-byte delimiter. In a single-quoted string only ' is doubled; in a double-quoted string every backslash is doubled.

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

:s/PATTERN/REPLACEMENT/FLAGSTEXT =~? PATTERNTEXT =~# PATTERN:set ignorecase Flags outside the patternWhere flags such as case-insensitive go when they are not written inside the pattern.

Case is the only matching flag: :s takes i (ignore case) and I (match case), =~? ignores case and =~# matches case. Everything else follows the ignorecase option unless the pattern contains \c or \C.

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

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

Every command and function takes the pattern as a string; the function list has no compiled pattern object.

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

who has it
TEXT =~ PATTERNmatch(TEXT, PATTERN)matchstr(TEXT, PATTERN)/PATTERN Search a textTest whether the pattern matches anywhere in a text, or get the first match.

None of these anchors. =~ gives true or false, match() the byte index or -1, matchstr() the matched text or an empty string; /PATTERN searches the buffer from the cursor.

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

matchstrlist([TEXT], PATTERN)matchbufline(bufnr(), PATTERN, 1, '$') Every matchGet all the matches that do not overlap.

Both return a list of dictionaries with text and byteidx; matchstrlist() takes a list of strings and matchbufline() reads buffer lines.

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

:s/PATTERN/REPLACEMENT/:%s/PATTERN/REPLACEMENT/gsubstitute(TEXT, PATTERN, REPLACEMENT, '')substitute(TEXT, PATTERN, REPLACEMENT, 'g') Replace matches

:s edits buffer lines and replaces the first match in each line of the range; the g flag replaces all of them. substitute() works on a string and replaces the first match with flags '', all with 'g'.

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

matchlist(TEXT, PATTERN)[NUM]submatch(NUM) Captured group, in codeRead what a group captured after a match. partial

By number only, groups 1 to 9; Vim patterns have no named groups. submatch(NUM) works only inside a \= replacement expression.

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

\NUM Captured group, in replacement text partial

By number only, \1 to \9; there are no named groups. A reference is always one digit, so a digit after it is literal text and no separator syntax exists.

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

&\0 Whole match, in replacement text

A literal ampersand is \&. With nomagic the two swap (\& is the match, & is literal); substitute() always uses the magic rules.

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 Vim

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 Vim

A string has no stored search position; match() and its relatives take an explicit {start} byte index. In a buffer the position is the cursor, which a failed search() leaves where it was.

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 Vim

There are no match variables to switch on. matchstrpos() returns the matched text with its start and end offsets.

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 Vim

Patterns are ordinary strings, so there is no interpolating literal and no such flag among the :s flags.

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 Vim

The manual lists execution of code in the regexp as unique to Perl. A replacement that starts with \= runs an expression, but that is in the replacement, not the pattern.

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 Vim

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