2 + 4
"Two plus four" consists of several constants or
literal values and an operator.
A calculator program must
recognize, for instance, that 2 is a numeric constant and
that the plus sign represents an operator, not to
be interpreted as the
An expression tells the computer how to produce a result. Although it is the sum of "two plus four" that we really want, we don't simply tell the computer to return a six. We instruct the computer to evaluate the expression and return a value.
An expression can be more complicated than 2+4; in fact, it might consist of multiple simple expressions, such as the following:
2 + 3 * 4
A calculator normally evaluates an expression from left to right. However, certain operators have precedence over others: that is, they will be performed first. Thus, the above expression will evaluate to 14 and not 20 because multiplication takes precedence over addition. Precedence can be overridden by placing the simple expression in parentheses. Thus, (2+3)*4 or "the sum of two plus three times four" will evaluate to 20. The parentheses are symbols that instruct the calculator to change the order in which the expression is evaluated.
A regular expression, by contrast, is descriptive of a pattern or sequence of characters. Concatenation is the basic operation implied in every regular expression. That is, a pattern matches adjacent characters. Look at the following example of a regular expression:
Each literal character is a regular expression that
matches only that single character.
This expression describes an "
A followed by a
B then followed by an
or simply the string
The term "string" means each character concatenated to the one
That a regular expression describes a sequence of characters
can't be emphasized enough.
(Novice users are inclined to think
in higher-level units such as words, and not individual characters.)
Regular expressions are case-sensitive;
A does not match
Programs such as
illustrates this process, trying to match the pattern
on an input line.
A regular expression is not limited to literal characters.
There is, for
instance, a metacharacter - the dot (
.) - that can be used as a
"wildcard" to match any single character.
You can think of this wildcard as analogous to a blank tile
in Scrabble(TM) where it means any letter.
Thus, we can specify the regular expression
A.E and it will
It will match any character
in the position following
* (the asterisk) is used to match zero or more
occurrences of the preceding regular expression, which typically
is a single character.
You may be familiar with
* as a shell
metacharacter, where it also means "zero or more characters."
But that meaning
is very different from
* in a regular expression.
By itself, the
* does not match anything in a regular expression;
it modifies what goes before it.
The regular expression
.* matches any number of
The regular expression
A.*E matches any string that matches
A.E but it will also
match any number of characters between
HOME, for example.
If you understand the difference between
* in regular
expressions, you already know about the two basic types of metacharacters: those
that can be evaluated to a single character, and those that modify how
characters that precede it are evaluated.
It should also be apparent that by use of metacharacters you can expand or limit the possible matches. You have more control over what is matched and what is not. In article 26.4, Bruce Barnett explains in detail how to use regular expression metacharacters.
- from O'Reilly & Associates' sed & awk
|26.2 Don't Confuse Regular Expressions with Wildcards||26.4 Using Metacharacters in Regular Expressions|