|
|
Wildcard regular expressions are useful for selecting files, but they cannot search the text within files. For that, you need to use the editor regular expressions. These are as follows:
This is equivalent to the wildcard ``?''. For example, .iddle will match ``diddle'', ``middle'', or any other word beginning with some letter followed by the string ``iddle''.
For example,
.iddle
matches:
iddle
middle
twiddle
As a single character is taken to be a literal regular expression matching only itself, this means that a character followed by an asterisk matches zero or more instances of itself. Consequently, ``.'' matches zero or more repeating instances of any character, and ``a'' matches zero or more ``a''s in a row.
Note that this behavior is not the same as that of the asterisk wildcard character. The shell interprets the asterisk wildcard to mean ``zero or more characters''; in an editor regular expression, the asterisk matches zero or more instances of the preceding regular expression.
Note that, like the asterisk, this editor regular expression metacharacter does not have the same effect as its wildcard counterpart, which matches a single character, not an instance of a preceding regular expression.
There is a subtle difference between the interpretation of regular
expressions containing a ``'' and a ``+''. For example,
suppose we have the word list:
fred
frog
figment
fuddled
ford
The expression ``fr+'' will match only ``fred'' and ``frog'', because it is constrained to match an ``f'' followed by at least one ``r''. However, ``fr'' will match all of these words, because it matches an ``f'' followed by zero or more instances of the letter ``r''.
If you are not certain of the spelling of a word that you are
searching for, this construction comes in handy. For example,
rel[ae]v[ae]nt
matches any of:
relavant
relavent
relevant
relevent
In the next specification, the ^ is a literal:
The ^ character is octal ASCII 136
In the following, the dollar is used to match a string occurring at
the end of a line:
It's the end of the line, folks$
In the next example, $ is a literal:
He stole $50000