Next: Examples
Up: Text Search
Previous: Text Search
Regular expression are a lot more powerful though. We can search for
lines containing the word 'file' not preceeded by 'old'. We can search
for empty lines, lines which contain one word or another. To be able
to do this it is necessary to understand the syntax and grammar of
regular expressions. A regular expression is always tried to be
matched against some text. If a match succeeds grep will print that
line.
- A '.' can stand for any character
- A list of characters enclosed by '
[
' and ']
' matches any single
character in that list. If the first character in the list is a '^ ',
then it matches any single character not in the list.
- The '^ ' matches the beginning of a line and the '$' matches the
end of a line.
- To match one or another regular expression,both regular
expressions can be bracketed with '(' and ')' and separated by
'|'.
- A regular expression is optional and matched at most once, if it
is followed by a '?'.
- A regular expression is matched zero or many times if
is followed by a '*'.
- A regular expression is matched one or many times if
is followed by a '+'.
- A regular expression is matched n times if
is followed by a '{n}'.
- A regular expression is matched n or more times if
is followed by a '{n,}'.
- A regular expression is matched m or less times if
is followed by a '{,m}'.
- A regular expression is matched at least n times and at
most m times if
is followed by a '{n,m}'.
In some versions of grep the following characters have to be
preceeded by a '\
': ?, +, {, |, (, ).
Mark O. Stitson
Wed Sep 25 10:45:32 BST 1996