Regular expressions

Characters

Use the following expressions to match individual letters, digits or whitespace characters.

Character

Description

Example

Sample match

\d

One digit from 0 to 9

file_\d\d

file_25

\w

Word character: ASCII letter, digit or underscore

\w-\w\w\w

A-b_1

\s

Whitespace character: space, tab, newline, carriage return, vertical tab

a\sb\sc

a b c

\D

One character that is not a digit as defined by \d

\D\D\D

ABC

\W

One character that is not a word character as defined by \w

\W\W\W\W\W

*-+=)

\S

One character that is not a whitespace character as defined by \s

\S\S\S\S

Yoyo

.

Any character except line break

a.c

abc

.

Any character except line break

.*

anything

\.

A period (special character: needs to be escaped by a \)

a\.c

a.c

\

Escapes a special character

\.\*\+\? \$\^\/\\

.*+? $^/

\

Escapes a special character

\[\{\(\)\}\]

[{()}]

Character classes

Character

Description

Example

Sample match

[ ]

One of the characters in the brackets

[AEIOU]

One uppercase vowel

[ ]

One of the characters in the brackets

T[ao]p

Tap or Top

-

Range indicator

[a-z]

One lowercase letter

[x-y]

One of the characters in the range from x to y

[A-Z]+

GREAT

[ ]

One of the characters in the brackets

[AB1-5w-z]

One of either: A,B,1,2,3,4,5,w,x,y,z

[x-y]

One of the characters in the range from x to y

[ -~]+

Characters in the printable section of the ASCII table.

[^x]

One character that is not x

[^a-z]{3}

A1!

[^x-y]

One of the characters not in the range from x to y

[^ -~]+

Characters that are not in the printable section of the ASCII table.

[\d\D]

One character that is a digit or a non-digit

[\d\D]+

Any characters, including new lines, which the regular dot doesn’t match

[\x41]

Matches the character at hexadecimal position 41 in the ASCII table, i.e. A

[\x41-\x45]{3}

ABE

Logic

Logic

Description

Example

Sample match

|

Alternation / OR operand

22|33

33

( )

Capturing group

A(nt|pple)

Apple (captures “pple”)

\1

Contents of group 1

r(\w)g\1x

regex

\2

Contents of group 2

(\d\d)\+(\d\d)=\2\+\1

12+65=65+12

(?: )

Non-capturing group

A(?:nt|pple)

Apple

Quantifiers

You can append one of the following expressions to any character, character class or capturing group to match multiple occurrences.

Quantifier

Description

Example

Sample match

+

One or more

Version \d\.\d+

Version 7.30

{3}

Exactly three times

\D{3}

ABiC

{2,4}

Two to four times

\d{2,4}

156

{3,}

Three or more times

\w{3,}

regex_tutorial

*

Zero or more times

A*B*C*

AAACC

?

Once or none

plurals?

plural

Example for system names

Assuming we have the system name B01.01F.AHU01_SUP_TS_AI that is consisting of the following segments:

Segment

Description

Meaning

B01

Building / object

Building 01

01F

Location / floor

1st floor

AHU01

System / trade

Air handling unit no. 1

SUP

System section

Supply air

TS

Component / function

Temperature sensor

AI

Data point type

Analog input

You can write different regular expressions to match different kinds of system names.

Example

Description

B\d\d\.\d\dF\.\w{3}\d{2}_\w{3}_\w{2}_\w{2}

This matches any data point in any building.

B01.0[1-3]F\.\w{3}\d{2}_\w{3}_\w{2}_\w{2}

This matches any data point on floors 1–3 in building 1.

B01.01F\.\w{3}\d{2}_(SUP|EXH}_\w{2}_\w{2}

This matches any supply air or exhaust air data point in building 1.

Note

To write and test regular expressions more easily, you can use external tools, such as regex101. Make sure to select the “Java” flavor.