Skip to the content of the web site.

5. Regular expressions

IEEE Spectrum Magazine    MIT Technology Review    Wired Magazine    EDN Network    EE Times    Skeptic's Guide to the Universe

We have already introduced searches, but it is possible to describe patterns that can be searched for. For example, you may want to search for either hello or Hello.

1. Matching one of many characters

If in a position, you want to have any one of many characters, you can use square brackets:

	[Hh]ello

You can include any number of characters between the brackets, for example,

	[Hh][aiou]t

which will match any of Hat, hat, Hit, hit, Hot, hot, Hut or hut.

Sometimes, it may be tedious to write a lot of numbers or letters, so you can also specify a range and vi will correctly interpret the range:

	[b-dfghj-np-tv-z]

This will search for all non-vowel letters (assuming that y is not a vowel).

If you want to search for a number or range of digits, you can do the same operation. The following will find any character that may appear in a hexadecimal number:

	[0-9a-fA-F]	

If the first character inside a bracket is a carat (^), then it says match any character not inside these brackets. If you want to search for, amoung other letters, a carat, just don't make it the first letter in the brackets or you have to escape it. The first of these matches any character other that d, while the next two match either a d or a ^:

	[^d]
	[d^]
	[\^d]

If you want to match any character, use ., while if you want to search for a period, use \., where \ is an escape character.

2. Matching identified groups of characters

The following six escaped letters will or will not match letters of the alphabet:

\aAny alphabetic character[a-zA-Z]
\AAny non-alphabetic character[^a-zA-Z]
\lAny lower-case character[a-z]
\LAny non-lower-case character[^a-z]
\uAny upper-case character[A-Z]
\UAny non-upper-case character[^A-Z]

The following four escaped letters will or will not match letters that may appear in variable names (variables names may start with a letter or an underscore, and all subsequent characters may be letters, underscores or digits):

\wAny character that appears in a variable name[_A-Za-z0-9]
\WAny character that cannot appear in a variable name[^_A-Za-z0-9]
\hAny character that can appear at the start of a variable name[_A-Za-z]
\HAny character that cannot appear at the start of a variable name[^_A-Za-z]

The following six escaped letters will or will not match digits:

\dAny decimal digit[0-9]
\DAnything that is not a decimal digit[^0-9]
\xAny hexadecimal digit[0-9a-fA-F]
\XAnything that is not a hexadecimal digit[^0-9a-fA-F]
\oAny octal digit[0-7]
\OAnything that is not a octal digit[^0-7]

3. Repeating a match

Suppose you want to search for either color or colour. Or perhaps you would like to match a variable name, which must have one character that must appear at the start of the variable, followed by zero or more subsequent characters. The First character cannot be a number, while the subsequent characters can be numbers as well.

Regular expressions in vi allow you to specify how often a pattern is matched. We will represent any other regular expression matching a character as /re/, and now:

/re/*Match the previous regular expression zero or more times0x\d*\.Match a 0x followed by zero or more digits followed by a period.
/re/\+Match the previous regular expression one or more times0x\d\+\.Match a 0x followed by zero or more digits followed by a period.
/re/\=Match the previous regular expression either zero or once.colou\=rMatch color or colour.
/re/\{n}Match the regular expression exactly n timesd[0-3]\{4}Match a d followed by four digits from 0 to 3.
/re/\{m,n}Match the regular expression between m and n timesd[0-3]\{4,7}Match a d followed by four to seven digits from 0 to 3.
/re/\{,n}Match the regular expression between 0 and n timesd[0-3]\{,7}Match a d followed by up to seven digits from 0 to 3.
/re/\{m,}Match the regular expression at least m timesd[0-3]\{6,}Match a d followed by at least six digits from 0 to 3.