Tag Archives: RegExp

My little regular expression “cheat sheet”

Regular expressions kan solve a whole lot of problems but remembering how to build them can be a real pain. This is my own “Cheat sheet” (tested in Vim v7.2)

. = match any wildcard character (must be one and one only)
c.t will match ‘cat’ but not ‘caaaat’ or ‘ct’

+= match 1 or more wildcard characters
ca+t will match ‘cat’, ‘caaat’ but not ‘ct’

* = match 0 or more of the preceeding character
ca*t will match ‘cat’, ‘caaaat’ and ‘ct’

? = match 0 or 1 of the preceding character
ca?t will match ‘cat’ and ‘ct’ but not ‘caaaat’

[] = match a range
[a-z] match all characters from a to z
[blke] match all of the letters b, l, k and e
[^blke] matches all characters EXCEPT b, l, k, and e

| = or character
cat|dog match either the full word ‘cat’ OR ‘dog’

^ = matches beginning of the string
^dog matches the first appearance of ‘dog’ in ‘dog dog dog’. Match is made on each line

$ = matches end of the string
dog$ matches all lines that end with ‘dog’. NOTE Whitespace is also a character!

\ = normal escape character
In Vim the following needs to be escaped to work: +, ?, | and {
A complete list can be found in Vim help ‘:help magic’

{n} = match preceding characters n times
[1-9]{6}-[1-9]{4} matches 771122-5748

{n,m} = match preceding characters between n and m times
‘ca{2,3}t’ will find ‘caat’ and ‘caaat’ but NOT ‘cat’ or ‘caaaat’