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.
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.
The following six escaped letters will or will not match letters of the alphabet:
\a | Any alphabetic character | [a-zA-Z] |
\A | Any non-alphabetic character | [^a-zA-Z] |
\l | Any lower-case character | [a-z] |
\L | Any non-lower-case character | [^a-z] |
\u | Any upper-case character | [A-Z] |
\U | Any 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):
\w | Any character that appears in a variable name | [_A-Za-z0-9] |
\W | Any character that cannot appear in a variable name | [^_A-Za-z0-9] |
\h | Any character that can appear at the start of a variable name | [_A-Za-z] |
\H | Any 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:
\d | Any decimal digit | [0-9] |
\D | Anything that is not a decimal digit | [^0-9] |
\x | Any hexadecimal digit | [0-9a-fA-F] |
\X | Anything that is not a hexadecimal digit | [^0-9a-fA-F] |
\o | Any octal digit | [0-7] |
\O | Anything that is not a octal digit | [^0-7] |
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 times | 0x\d*\. | Match a 0x followed by zero or more digits followed by a period. |
/re/\+ | Match the previous regular expression one or more times | 0x\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\=r | Match color or colour. |
/re/\{n} | Match the regular expression exactly n times | d[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 times | d[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 times | d[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 times | d[0-3]\{6,} | Match a d followed by at least six digits from 0 to 3. |