References > Aliases > Regular Expression Pattern Matching

Regular Expression Pattern Matching

When editing comparison criteria, the following regular expressions can be used for pattern matching. Examples are provided at the end of this section.

Characters

Character

Description

Example

Any character except [,\,^,$,.,|,?,*,+,(,),

All characters except the listed special characters match a single instance of themselves.

a matches a

\ (backslash) followed by any of [,\,^,$,.,|,?,*,+,(,),

A backslash escapes special characters to suppress their special meaning.

\+ matches +

\xFF where FF are 2 hexadecimal digits

Matches the character with the specified ASCII/ANSI value, which depends on the code page used. Can be used in character classes.

\xA9 matches © when using the Latin-1 code page.

\n, \r and \t

Match an LF character, CR character and a tab character respectively. Can be used in character classes.

\r\n matches a DOS/Windows CRLF line break.

Character Classes or Character Sets [abc]

Character Classes or Sets

Description

Example

[ (opening square bracket)

Starts a character class. A character class matches a single character out of all of the possibilities offered by the character class. Inside a character class, different rules apply. The rules in this section are only valid inside character classes. The rules outside this section are not valid in character classes, except \n, \r, \t and \xFF

Any character except ^,-,],\ add that character to the possible matches for the character class.

All characters except the listed special characters.

[abc] matches a, b or c

\ (backslash) followed by any of ^,-,],\

A backslash escapes special characters to suppress their special meaning.

[\^\]] matches ^ or ]

- (hyphen) except immediately after the opening [

Specifies a range of characters. (Specifies a hyphen if placed immediately after the opening [)

[a-zA-Z0-9] matches any letter or digit

^ (caret) immediately after the opening [

Negates the character class, causing it to match a single character not listed in the character class. (Specifies a caret if placed anywhere except after the opening [)

[^a-d] matches x (any character except a, b, c or d)

\d, \w and \s

Shorthand character classes matching digits 0-9, word characters (letters and digits) and whitespace respectively. Can be used inside and outside character classes

[\d\s] matches a character that is a digit or whitespace

Anchors

Anchors

Description

Example

^ (caret)

Matches at the start of the string to which the regular expression pattern is applied. Matches a position rather than a character. Most regular expression flavors have an option to make the caret match after line breaks (i.e. at the start of a line in a file) as well.

^. matches a in abc\ndef. Also matches d in "multi-line" mode.

$ (dollar)

Matches at the end of the string to which the regular expression pattern is applied. Matches a position rather than a character. Most regular expression flavors have an option to make the dollar match before line breaks (i.e. at the end of a line in a file) as well. Also matches before the very last line break if the string ends with a line break.

.$ matches f in abc\ndef. Also matches c in "multi-line" mode.

\A

Matches at the start of the string to which the regular expression pattern is applied to. Matches a position rather than a character. Never matches after line breaks.

\A. matches a in abc

\Z

Matches at the end of the string to which the regular expression pattern is applied. Matches a position rather than a character. Never matches before line breaks, except for the very last line break if the string ends with a line break.

.\Z matches f in abc\ndef

\z

Matches at the end of the string to which the regular expression pattern is applied. Matches a position rather than a character. Never matches before line breaks.

.\z matches f in abc\ndef

Quantifiers

Quantifiers

Description

Example

? (question mark)

Makes the preceding item optional. The optional item is included in the match, if possible.

abc? matches ab or abc

??

Makes the preceding item optional. The optional item is excluded in the match, if possible. This construct is often excluded from documentation due to its limited use.

abc?? matches ab or abc

* (star)

Repeats the previous item zero or more times. As many items as possible will be matched before trying permutations with fewer matches of the preceding item, up to the point where the preceding item is not matched at all.

.* matches "def" "ghi" in abc "def" "ghi" jkl

*? (lazy star)

Repeats the previous item zero or more times. The engine first attempts to skip the previous item before trying permutations with ever increasing matches of the preceding item.

.*? matches "def" in abc "def" "ghi" jkl

#NAME?

Repeats the previous item once or more. As many items as possible will be matched before trying permutations with fewer matches of the preceding item, up to the point where the preceding item is matched only once.

.+ matches "def" "ghi" in abc "def" "ghi" jkl

+? (lazy plus)

Repeats the previous item once or more. The engine first matches the previous item only once, before trying permutations with ever increasing matches of the preceding item.

.+? matches "def" in abc "def" "ghi" jkl

{n} where n is an integer >= 1

Repeats the previous item exactly n times.

a{3} matches aaa

{n,m} where n >= 1 and m >= n

Repeats the previous item between n and m times. Will try to repeat m times before reducing the repetition to n times.

a{2,4} matches aa, aaa or aaaa

{n,m}? where n >= 1 and m >= n

Repeats the previous item between n and m times. Will try to repeat n times before increasing the repetition to m times.

a{2,4}? matches aaaa, aaa or aa

{n,} where n >= 1

Repeats the previous item at least n times. Will try to match as many items as possible before trying permutations with fewer matches of the preceding item, up to the point where the preceding item is matched only m times.

a{2,} matches aaaaa in aaaaa

{n,}? where n >= 1

Repeats the previous item between n and m times. The engine first matches the previous item n times before trying permutations with ever increasing matches of the preceding item.

a{2,}? matches aa in aaaaa

 

Dot

Dot Character

Description

Example

. (dot)

Matches any single character except line break characters \r and \n.

. matches x or most any other character

 

Word Boundaries

Word Boundary

Description

Example

\b

Matches at the position between a word character (anything matched by \w) and a non-word character (anything matched by [^\w] or \W) as well as at the start and/or end of the string if the first and/or last characters in the string are word characters.

.\b matches c in abc

\B

Matches at the position between two word characters (i.e the position between \w\w) as well as at the position between two non-word characters (i.e. \W\W).

\B.\B matches b in abc

Alternation

Alternation Character Description Example

|

(vertical bar or “pipe”)

Causes the regular expression engine to match either the part on the left side or the part on the right side. Can be strung together into a series of options.

abc|def|xyz matches abc, def or xyz

|

(vertical bar or “pipe”)

The vertical bar has the lowest precedence of all operators. Use grouping to alternate only part of the regular expression.

abc(def|xyz) matches abcdef or abcxyz

Regular Expression Pattern Matching Examples

The following examples illustrate uses of regular expression pattern matching:

snmp-server community public

 

Finds any line that includes the text snmp-server community public. There can be text before and/or after the string on the same line. service tcp-keepalives-in.*\n(.*\n)*.*service tcp-keepalives-out

 

Finds the first line service tcp-keepalives-in and then looks for service tcp-keepalives-out on any line after that. The regular expression string .*\n(.*\n)*.* is used to search any number of lines between strings. access-list 105 deny.*tcp any any eq 139 log

 

Finds the line with access-list 105 deny, followed by any number of characters of any type, followed by tcp any any eq 139 log on the same line. The regular expression string .* finds any character and any number of characters on the same line. This expression can be used to find spaces, tabs, numbers, letters, or special characters. ntp clock-period \d*


Finds any line that includes ntp clock-period, followed by any number. The regular expression string \d* will find any number at any length, such as 3, 48, or 2394887.

user \x2a

 

Finds any line that includes user *. The regular expression string \x, followed by a hexadecimal value, specifies an individual character. In this example, \x2a represents the asterisk character, which has a hexadecimal value of 2a.