Introducing UNIX and Linux |
Overview |
Basic regular expressionsThe general idea is just like pattern matching - a BRE consists of a sequence of characters, some of which have a special meaning. The BRE is said to match a string if
In order to check whether a BRE matches a string, the two
strings are examined working from left to right. Each time a match
is found, the corresponding parts of the BRE and the string are
discarded and the process continues immediately after. First of
all, we consider how to specify a match for a single
character. For this we use a BRE called a a bracket
expression, which is an expression enclosed in square
brackets (
and a character matches a matching list if it matches any of the
patterns that make up that sequence. The following BRE matches the
letters [ax-z[:digit:]] If a matching list is preceded by a circumflex (^) it becomes a
nonmatching list, and matches any character not specified in that
list. So [^[:upper:]#] will match any character that is neither an upper-case letter
nor the symbol [-xyz] will match [Cc]hris will match [[:alpha:]].. will match any 3-character string commencing with a letter. More
generally, if you follow a bracket expression (or a single
character or a dot) with an asterisk ( [[:digit:]][[:digit:]][[:digit:]]* will match any string consisting of two or more digits. The two characters ^ and $ are used to indicate the start and end of a string respectively, so ^A.*E$ will match any string commencing with Worked exampleWhat BRE will match a string that is just a sequence of
digits?
|
Copyright © 2002 Mike Joy, Stephen Jarvis and Michael Luck