beautypg.com

13 regular expressions, Overview, 13 regular – H3C Technologies H3C Intelligent Management Center User Manual

Page 134: Expressions

background image

125

13 Regular expressions

Overview

A regular expression is a sequence of characters that forms a search pattern for string matching. A

regular expression contains the following elements:

Regular characters (letters and digits) with literal meanings.

Metacharacters with special meanings.

Table 1

lists common metacharacters and their meanings.

Table 1 Metacharacters in a regular expression

Metacharacters Meaning Examples

^

Matches the beginning of a line.

"^display$" matches "display".

$

Matches the end of a line.

*

Matches the preceding character or
string zero, one, or multiple times.

"zo*" matches "z", "zo", and "zoo".

+

Matches the preceding character or
string one or multiple times.

"zo+" matches "zo" and "zoo", but not "z".

( )

Matches the string in the parentheses.

"(ABC)" matches "ABC".
"A(BC)+" matches "ABC" and "ABCBC".

?

Matches the preceding character or
string zero or one time.

"do(es)?" matches "do" and "does".

{n}

Matches the preceding character n
times. The number n must be a

nonnegative integer.

"fo{2}d" matches "food", but not "fod".

{n,}

Matches the preceding character n
times or more. The number n must be

a nonnegative integer.

"fo{2,}d" matches "food" and "foood", but not
"fod".

{n,m}

Matches the preceding character n to
m times or more. The numbers n and
m must be nonnegative integers and n

cannot be greater than m.

"fo{2,3}d" matches "food", and "foood", but not
"fod".

.

Matches any single character except
\n.

N/A

x|y

Matches x or y.

"m|pay" matches "m" and "pay". "(m|p)ay"
matches "may" and "pay".

[xyz]

Matches a single character in the
brackets: x, y, or z.

"a[xyz]" matches "ax", "ay", and "az".

[^xyz]

Matches a single character that is not
in the brackets.

"a[^xyz]" matches "aa", "ab", and "az", but not
"ax", "ay", or "az".