Regular expression examples

These examples demonstrate how to use regular expressions to perform pattern matching.

Table 1 provides a set of regular expression examples, together with sample strings as well as the results of applying the regular expression to those strings.

There are two important cases in matching regular expressions with strings. A regular expression may match an entire string (a case known as a string match) or only a part of that string (a case known as a sub-string match). For example, the regular expression \<int\> will generate a sub-string match for the string int x but will not generate a string match. This distinction is important because some subagents do not support sub-string matching. Where applicable, the results listed in the examples differentiate between string and sub-string matches.

Table 1. Regular expression examples

This expression...

Applied to this string...

Results in...

.

a

String match

!

String match

abcdef

Sub-string match on a

empty string

No match

M..COUNT

MINCOUNT

String match

MXXCOUNTY

Sub-string match on MXXCOUNT

NONCOUNT

No match

.*

empty string

String match

Animal

String match

.+

Any non-empty string

String match

empty string

No match

^

empty string

String match

hello

Sub-string match of length 0 at position 0 (position 0 = first character in string)

$

empty string

String match

hello

Sub-string match of length 0 at position 5 (position 0 = first character in string)

^$

empty string

String match

hello

No match

\bee

tee

No match

Paid fee

No match

feel

No match

eel

Sub-string match on ee

.*thing.*

The thing is in here

String match

there is a thing

String match

it isn't here

No match

thinxxx

No match

a*

empty string

String match

aaaaaaaaa

String match

a

String match

aardvark

Sub-string match on aa

this string

Sub-string match

((ab)*c)*

empty string

String match

ccccccccc

String match

ccccabcccabc

String match

a+

empty string

No match

aaaaaaaaa

String match

a

String match

aardvark

Sub-string match on aa

this string

No match

((ab)+c)*

empty string

String match

ababababcabc

String match

(ab){2}

abab

String match

cdabababab

Sub-string match on abab

ab

No match

[0-9]{4,}

123

No match

a1234

Sub-string match on 1234

a{0}

empty string

String match

a

No match

hello

Sub-string match of length 0 at position 0 (position 0 = first character in string)

[0-9]{1,8}

this is not a number

No match

a=4238, b=4392876

Sub-string match on 4238

([aeiou][^aeiou])+

Hello

Sub-string match on el

!!! Supacalafraglistic

Sub-string match on upacalaf

[+-]?1

1

String match

+1

String match

-1

String match

.1

Sub-string match on 1

value+1

Sub-string match on +1

a¦b

a

String match

b

String match

c

No match

Daniel

Sub-string match on a

abcd¦efgh

abcd

String match

efgh

String match

abcdfgh

Sub-string match on abcd

[0-9A-F]+

BAADF00D

String match

C

String match

baadF00D

Sub-string match on F00D

c

No match

G

No match

g

No match

x = \d+

x = 1234

String match

x = 0

String match

x = 1234a

Sub-string match on x = 1234

x = y

No match

x^=^ where ^ represents a space character

No match

\D\d

a1

String match

a11

Sub-string match on a1

-9

String match

a

No match

8

No match

aa

No match

4t

No match

\s+

Hello_w0rld

No match

Hello^^^world where ^ represents a space character

Sub-string match on ^^^ where ^ represents a space character

Widget^ where ^ represents a space character

Sub-string match on ^ where ^ represents a space character

^^^^^ where ^ represents a space character

String match

\S+

Hello_w0rld

Sub-string match of length 11 on Hello_w0rld

Hello^^^world where ^ represents a space character

Sub-string match on Hello

Widget^ where ^ represents a space character

Sub-string match on Widget

^^^^^ where ^ represents a space character

No match

\w+

D4n_v4n Vugt

Sub-string match on D4n_v4n

^^^hello where ^ represents a space character

Sub-string match on hello

blah

String match

x#1

No match

foo bar

No match

\W

Hello there

Sub-string match of length 1 on separating space character

~

String match

aa

No match

a

No match

_

No match

^^^^444 == 5 where ^ represents a space character

Sub-string match of length 1 on first ^ where ^ represents a space character

\w+\s*=\s*\d+

x = 123

String match

count0=555

String match

my_var=66

String match

0101010=0

String match

xyz = e

No match

delta=

No match

==8

No match

[[:alnum:]]+

1234

String match

...D4N13L

Sub-string match on D4N13L

[[:alpha:]]+

Bubble

String match

...DANI3L

Sub-string match on DANI

69

No match

[[:blank:]]+

alpha^^^^and beta where ^ represents a space character

Sub-string match on ^^^^ where ^ represents a space character

Animal

No match

empty string

No match

[[:space:]]+

alpha^^^^and beta where ^ represents a space character

Sub-string match on ^^^^ where ^ represents a space character

Animal

No match

empty string

No match

[[:cntrl:]]+

...Hello W0rld!

No match

empty string

No match

[[:graph:]]+

hello world

Sub-string match on hello

^^^^^ where ^ represents a space character

No match

^^^!? where ^ represents a space character

Sub-string match on !?

[[:lower:]]+

Animal

Sub-string match on nimal

ABC

No match

0123

No match

foobar

String match

^^^0blaH! where ^ represents a space character

Sub-string match on bla

[_[:lower:]]+

foo_bar

String match

this_thinG!!!

Sub-string match on _thin

[[:upper:]]+

YES

String match

#define MAX 100

Sub-string match on MAX

f00 b4r

No match

[[:print:]]+

hello world

String match

^^^^^ where ^ represents a space character

String match

[[:punct:]]+

didn't

Sub-string match on '

Animal

No match

[[:xdigit:]]+

43298742432392187ffe

String match

x = bAAdF00d

Sub-string match on bAAdF00d

4327afeffegokpoj

Sub-string match on 4327afeffe

c:\\temp

c:\temp

String match