Regular expressions


A regular expression is a string with special meanings assigned to certain characters. For example, the period ( . ) means match any character, and asterisk ( * ) means match zero or more of the previous character.

Regular expression characters can be combined to form more complex expressions. The expression "The .* dog" will match "The lazy dog" and "The brown dog" since ".*" will match zero or more ( * ) of any character ( . ).

Related topics

The table below describes many of the regular expression characters and how they are used to match the input string.

Groups may be extracted using the group notation in the regular expression. A group is surrounded by parentheses - "(" and ")" in the regular expression. An expression to extract the kind of dog would look like "The (.*) dog". The result has a GROUPS array containing a single element indicating what kind of dog was described in the string.

Characters

Description

x

The character x

\\

The backslash character

\t

The tab character ( '\u0009')

\n

The newline (line feed) character ( '\u000A')

\r

The carriage-return character ( '\u000D')

\f

The form-feed character ( '\u000C')

\e

The escape character ( '\u001B')

Character classes

Description

[abc]

a, b, or c (simple class)

[^abc]

Any character except a, b, or c (negation)

[a-zA-Z]

a through z or A through Z, inclusive (range)

[a-d[m-p]]

a through d, or m through p: [a-dm-p] (union)

[a-z&&[def]]

d, e, or f (intersection)

[a-z&&[^bc]]

a through z, except for b and c: [ad-z] (subtraction)

[a-z&&[^m-p]]

a through z, and not m through p: [a-lq-z](subtraction)

Predefined character classes

Description

.

Any character (may or may not match line terminators)

\d

A digit: [0-9]

\D

A non-digit: [^0-9]

\s

A whitespace character: [\t\n\x0B\f\r]

\S

A non-whitespace character: [^\s]

\w

A word character: [a-zA-Z_0-9]

\W


Boundary matchers

Description

^

The beginning of a line

$

The end of a line

\b

A word boundary

\B

A non-word boundary

\A

The beginning of the input

\G

The end of the previous match

\Z

The end of the input but for the final terminator, if any

\z

The end of the input

Greedy quantifiers

Description

X ?

X, once or not at all

X *

X, zero or more times

X +

X, one or more times

X { n }

X, exactly n times

X { n ,}

X, at least n times

X { n , m }

X, at least n but not more than m times

Reluctant quantifiers

Description

X ??

X, once or not at all

X *?

X, zero or more times

X +?

X, one or more times

X { n }?

X, exactly n times

X { n ,}?

X, at least n times

X { n , m }?

X, at least n but not more than m times

Possessive quantifiers

Description

X ?+

X, once or not at all

X *+

X, zero or more times

X ++

X, one or more times

X { n }+

X, exactly n times

X { n ,}+

X, at least n times

X { n , m }+

X, at least n but not more than m times

Example:

string = "MVCM"

string1 = "BMC Software"

string2 = "CCS Solution"

result1 = regex(string, "^MVCM$")

// Exact match - result1.MATCHES will be true

if result1.MATCHES == true then

// Do the work here.

endif

result2 = regex(string1, "BMC")

// Partial match

if result2.MATCHES == true then

// Do the work here.

endif

result3 = regex(string2, "^CCS.*")

// Starts with "CCS" with anything after that

if result3.MATCHES == true then

//88

// Do the work here.

endif

string3 = "The lazy dog"

result4 = regex(string3, "The (.*) dog")

if result4.MATCHES == true then

kindofdog = result4.GROUPS[0]

//

endif

 

Tip: For faster searching, add an asterisk to the end of your partial query. Example: cert*