Perl Regular Expressions

Regexes

Regular expressions (commonly called regexes) are tools used for pattern matching. They are useful to find the answer to questions like, "Is this text like such-and-such", or for queries like, "Find me all items like this-and-that".

SevOne NMS uses regular expressions throughout the application and many fields enable you to enter your own regular expressions.

Regular Expressions

Each kind of regular expression has its own style and rules. For example, DOS-style regular expressions perform wildcard matching:

Character Meaning
* Matches any number of characters (including zero)
? Matches any one character

And SQL supports the following wildcards:

Character Meaning
% Matches any number of characters (including zero)
_ Matches any one character

BASH regular expressions are more powerful. Linux supports both * and ? plus the idea of a character class.

Character Meaning
* Matches any number of characters (including zero)
? Matches any one character
[a-zA-Z] Matches any English letter

Perl Regular Expressions

Some of the most widely used regular expressions are Perl regular expressions that were used internally by the Perl scripting language. Perl regular expressions use a combination of normal and special characters to define match statements. By default, they operate on one line of text only. A line is delimited in one of two ways: by simply terminating; or by a carriage return or line feed combination (\r) and (\n).

Character Meaning
. Matches any single character
^ Matches the beginning of the string
$ Matches the end of the string
character Matches a specific character
[characters] Matches the class of characters or ranges of characters within the square brackets
| Delimiter between two strings to match
() Used to group a collection of items
{x,y}
To match between the xth and yth characters of a string. If the second number is left out, no limit is imposed
? To match one or none of something. This is equivalent to {0,1}.
+ To match one or more of something. This is equivalent to {1,}.
* To match any or none of something. This is equivalent to {0,}.
\ To match any of the special characters listed, you may prefix that character with a \. Also known as 'escaping' characters.

Examples

For a router with the name of RTR NYC 01, you could use the following to match that string exactly. This also matches Wireless RTR NYC 01.

RTR NYC 01

To match only the Wireless RTR NYC 01 string, you could use:

^Wireless RTR NYC 01$

To match any string that begins with Wireless, you could use:

^Wireless

To match all strings with Wireless or RTR in them, you could use:

Wireless|RTR

To match any IP address, you could use:

[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}

To match a DNS name, you could use:

([a-zA-Z0-9][a-zA-Z0-9-]*\.)+[a-zA-Z0-9-]