Skip to content

Instantly share code, notes, and snippets.

@tapyu
Last active May 27, 2024 18:19
Show Gist options
  • Save tapyu/26934ea1b36a6aef586184b3ecd9c888 to your computer and use it in GitHub Desktop.
Save tapyu/26934ea1b36a6aef586184b3ecd9c888 to your computer and use it in GitHub Desktop.
RegEx cheat sheet
command Ragex engine

SEARCH

Matching characters

Character class

\w .NET: a-z, A-Z, 0-9, _; Vim's regex engine(?)
[[:alpha:]] BRE, ERE, PCRE: a-z, A-Z
[[:lower:]] BRE, ERE, PCRE: a-z
[[:upper:]] BRE, ERE, PCRE: A-Z
[[:alnum:]] BRE, ERE, PCRE: a-z, A-Z, 0-9
[[:punct:]] BRE, ERE, PCRE: !,",#,$,%,&,',(,),*,+,,,.,/,:,;,<,=,>,?,@

Not a Word Character

\W ?

Whitespace

\s .NET (it matches space, tab, newline. Vim's regex engine: it matches only space or tab), PCRE
\n Vim's regex engine: it matches only a new line)
\s Vim's regex engine (It matches space, tab, or newline)

Not Whitespace (space, tab, newline)

\S ?

Tab

\t .NET

Digit (0-9)

\d .NET, PCRE
[[:digit:]] BRE, ERE, PCRE

Not a Digit (0-9)

\D ?

Any Character Except New Line

. BRE, ERE, PCRE, .NET, Vim's regex engine

Boundaries

Word Boundary (begging or end)

\b BRE, ERE, PCRE, .NET, Vim's regex engine(?)

Not a Word Boundary (begging or end)

\B BRE, ERE, PCRE, .NET, Vim's regex engine(?)

Beginning of a word

\< BRE, ERE, Vim's regex engine

End of a word

\> BRE, ERE, Vim's regex engine

Beginning of the line

^ BRE, ERE, PCRE, .NET, Vim's regex engine

End of the line

$ BRE, ERE, PCRE, .NET, Vim's regex engine(?)

Grouping and Matching in a range

Matches Characters within or in the range

[] BRE, ERE, PCRE, .NET, Vim's regex engine (matching characters do not work, use range instead)

Matches Characters NOT within or in the range

[^ ]

Either Or

| ERE, PCRE
\| BRE

Capturing group

( ) .NET, PCRE
\( \) BRE(?), Vim's regex engine

match the α-th capturing group again

.NET, PCRE

Noncapturing group

(?:) .NET

Negative lookahead noncapturing group for the previous character

EX: "q(?!u)" matches "q" that is not followed by "u"
(?!)

Positive lookahead noncapturing group for the previous character

EX: "q(?=u)" matches "q" that is followed by "u"
(?=)

Negative Lookbehind noncapturing group for the next character

EX: (?<!a)b matches a b that is not preceded by an a
(?<!) .NET

Positive Lookbehind noncapturing group for the next character

EX: (?<=a)b matches a b that is preceded by an a
(?<=) .NET

Quantifiers (applied to the preceded character)

0 or More (greedy)

* .NET, Vim's regex engine

0 or More (lazy)

*? .NET, Vim's regex engine(?)

1 or More (greedy)

\+ BRE(?), Vim's regex engine
+ .NET

1 or More (lazy)

\+? BRE(?), Vim's regex engine(?)
+? .NET

0 or One

? ERE, PCRE, .NET
\? BRE, Vim's regex engine

Range Number

{i,j} ERE, PCRE, .NET
\{i,j} Vim's regex engine
\{i,j\} BRE

REPLACE

Placeholder, where n is the number order of the placeholder

$n .NET
\ n Vim's regex engine

Newline

\ n .NET
\r Vim's regex engine

Tab key

\t .NET

Cause all characters of the next placeholder to be upper case

\U .NET

Cause the first characters of the first word of the next placeholder to be upper case

\u .NET

Cause all characters of the next placeholder to be lower case

\L .NET

Cause the first characters of the first word of the next placeholder to be lower case

\l .NET
OBS: you can mix them: Ex, you can use \u\L to make the first letter be upper case and the rest of the placeholder lowercase.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment