Library Table of Contents Index Previous Next Comments

Regular expressions

About regular expressions  Top

Regular expressions are familiar to those who have worked extensively with UNIX command-line tools. They form complex rules, or pattern descriptions which are used to match patterns against a string. Regular expressions are built by combining simple, small expressions, the simplest being a single character, such as "a", that matches itself. Simple regular expressions can be combined to form strings, such as "hello", which matches "hello" or "hihellothere".

Matching  Top

Regular expression matches compare a specified string against each pattern until a match occurs. When a match occurs, the function returns true. If no match occurs, the function is false. Rules are executed only when a pattern-matching expression is true.

Note
NetCache uses POSIX-standard extended regular expression syntax, fully described by IEEE POSIX 1003.2.

Performance and efficiency  Top

Regular expressions can be used to perform a variety of restrictions. However, rule complexity and the number of matches that are checked before servicing each request can affect performance. For this reason, regular expressions, or expressions that contain the word "matches", should be cautiously applied and used only when necessary.

Example

Values returned match any pattern that ends with netapp.com.

server-name matches ".*\.netapp\.com$"

In comparison, an equivalent but more efficient expression produces the same result.

deny server-domain netapp.com

Optimizing performance  Top

Complex regular expressions or expressions that perform extensive character matching consume the greatest amount of system resources, followed by simple expressions and exact matches. To optimize performance when expression matching is necessary, be sure that you form expressions using the correct variables. Often, a complex expression can be simplified to an exact match. For example, the following sets of expressions achieve the same goal.

Example 1

In the following example, url matches can be replaced with the server-name variable to achieve the same result.

Simple expression:

Exact match:

deny url matches "^[a-z]*://www.netapp\.com/.*"

deny server-name www.netapp.com

Example 2

In the following example, url matches can be replaced with the server-domain variable to achieve the same result.

Simple expression:

Exact match:

deny url matches "^[a-z]*://[^/]*jobs\.(com | net)"

deny server-domain "jobs.com" and

deny server-domain "jobs.net"

Example 3

In the following example, url matches can be replaced with either server-name or url-prefix variables to achieve the same result.

Complex expression:

deny url matches "^http://chat\.yahoo\.com/(fishing | music | cooking)$"

Exact match:

deny server-name chat.yahoo.com

In the preceding example, the entire site is blocked.

Exact match:

deny url-prefix http://chat.yahoo.com/fishing

deny url-prefix http://chat.yahoo.com/music

deny url-prefix http://chat.yahoo.com/cooking

Contains and contains-case operators  Top

Regular expressions are commonly used to match simple substrings in URLs. Alternatively, you can use the ACL contains and contains-case operators. These operators provide a more efficient method for matching URL substrings.

For example, the following regex expression denies any URL matches that contain the string "sports".

deny url matches ".*sports.*"

In comparison, either of the following ACL rules produces the same result as the preceding regex matching expression.

deny url contains sports

deny url contains-case SporTS

The contains and contains-case operators support URL substring matching for the following ACL variables:

Characters  Top

Characters that are commonly used to form regular expressions are shown in the following table.

Character

Description

.

Matches any single character.

*

Matches zero or more copies of the preceding expression.

[]

Matches any character within the brackets.

A "-" character can be used inside brackets to specify range, for example [0-7] is the same as [01234567].

A "^" character can be used inside brackets to negate the characters specified. For example, if the first character within the brackets is a "^", the call is to match any character except those specified within the brackets.

^

Matches the beginning of a line as the first character of a regular expression.

$

Matches the end of a line as the last character of a regular expression.

{}

Specifies how many times the previous pattern is allowed to match. For example {1,4} matches one to four occurrences of the letter A.

\

Escape. Used as part of the Ctrl-C escape sequence.

+

Matches one or more occurrence of the preceding regular expression. For example, [0,7]+ matches 1, 111, or 12345, but it does not match an empty string.

?

Matches zero or one occurrence of the preceding regular expression.

|

Matches the preceding regular expression or the following regular expression.

"..."

Interprets every character enclosed within the quotation marks literally.

()

Groups together a series of regular expressions.

Note
For more information, see the UNIX man page on regular expression handling (man 5 regexp). In addition, see the man pages for the UNIX ed editor (man ed) for descriptions of regular expressions.

Pseudogrammar  Top

The following table provides a variety of operators that can be used in creating complex expressions. Parsing is performed in a case-insensitive manner.

Operator

Example

AND

&&, and

OR

||, or

NOT

!, not

BITRATE

[0-9]* kbps/Mbps

REGCMP

=~, matches

TIME

[0-2] [0-9] (:[0-5] [0-9]) ? (am|pm)

DAY

(mon|tue|...), (monday|tuesday|...)

SUBNET

XXX.XXX.XXX.XXX/YY, XXX.XXX.XXX.XXX/YYY.YYY.YYY.YYY

ADDR-VAR

client-ip, server-ip

PORT-VAR

client-port, server-port

VAR

cache-ip, cache-port, client-ip, client-domain, client-port, date, day, group, req-headers, server-ip, server-domain, server-name, server-port, time, url, user

ACTION

allow, deny, redirect, rewrite, auth, re-auth, try-auth

EXPN

VAR REGCMP <regexp>

EXPN

ADDR-VAR SUBNET | ADDR-VAR <ip> | ADDR-VAR <ip-wildcard>

EXPN

any | auth | ftp | nntp | http | tunnel | accel | cacheable | cached | gopher | live | mms | rtsp | splittable | streaming

EXPN

PORT-VAR <port> | PORT-VAR [<port>] - [<port>]

EXPN

date <date> | date [<date>] - [<date>]

EXPN

day DAY - DAY | day DAY

EXPN

group <group-name>

EXPN

bitrate [BITRATE] - [BITRATE]

EXPN

server-name <name>

EXPN

smartfilter [<category-name>]

EXPN

wwfilter [<category-name>]

EXPN

time TIME - TIME

EXPN

url <url> url-prefix <prefix>

EXPN

user <user-name>

EXPN

EXPN and EXPN

EXPN

EXPN or EXPN

EXPN

(EXPN)

EXPN

NOT EXPN

RULE

ACTION EXPN

Library Table of Contents Index Previous Next Comments