Node:Regular expressions, Neste:Iterating over lists, Forrige:Quoted strings, Opp:More advanced concepts
Regular expressions can be used in cfengine in connection with
editfiles
and processes
to search for lines matching
certain expressions. A regular expression is a generalized wildcard. In
cfengine wildcards, you can use the characters '*' and '?' to match any
character or number of characters. Regular expressions are more
complicated than wildcards, but have far more flexibility.
NOTE: the special characters *
and ?
used in wildcards do not have the
same meanings as regular expressions!.
Some regular expressions match only a single string. For example, every
string which contains no special characters is a regular expression
which matches only a string identical to itself. Thus the regular
expression cfengine
would match only the string "cfengine", not
"Cfengine" or "cfengin" etc. Other regular expressions could match more
general strings. For instance, the regular expression c*
matches
any number of c's (including none). Thus this expression would match the
empty string, "c", "cccc", "ccccccccc", but not "cccx".
Here is a list of regular expression special characters and operators.
\
[\]
or quoted with a backslash itself \\
.
\b
\B
\<
\>
\w
\W
any character
.
*
c*
.
If no object precedes it, it represents a literal asterisk.
+
?
{ }
{5}
would match exactly 5
instances of the previous object. {6,}
would match at least
6 instances of the previous object. {7,12}
would match at least
7 instances of, but no more than 12 instances of the preceding object.
Clearly the first number must be less than the second to make a valid
search expression.
|
[list]
[a-z]
matches any character in the range a to
z, abcd
matches either a, b, c or d. Most characters are
ordinary inside a list, but there are some exceptions: ]
ends the
list unless it is the first item, \
quotes the next character,
[:
and :]
define a character class operator (see below),
and -
represents a range of characters unless it is the first
or last character in the list.
[^list]
[:class:]
alnum
alpha
blank
cntrl
digit
graph
lower
print
punct
space
upper
xdigit
( )
\digit
^
$
Here is a few examples. Remember that some commands look for a regular expression match of part of a string, while others require a match of the entire string (see Reference manual).
^# match string beginning with the # symbol ^[^#] match string not beginning with the # symbol ^[A-Z].+ match a string beginning with an uppercase letter followed by at least one other character