Examples - excluding a string

Using Regex for specific filter: Exclude a string

In this example we want to exclude everything that contains the string server.

To do this, we will use the following Regular Expression:

^((?!.*server.*).)*$

Breakdown of Regex elements used

^

Beginning of the string

()

Capturing Group

(?!)

Negative look ahead. Specifies a group that cannot match after the main expression.
(i.e. if it matches, then the result is discarded)

.

Matches any character except line breaks

*

Match 0 or more of the preceding token

server

String

$

End of the string


Note

You can get the same result by excluding each character of the string.

^((?![s][e][r][v][e][r]).)*$

^

Beginning of the string

()

Capturing Group

(?!)

Negative look ahead. Specifies a group that cannot match after the main expression.
(i.e. if it matches, then the result is discarded)

[]

Matches any character in the set

s

Character

e

Character

r

Character

v

Character

e

Character

r

Character

.

Matches any character except line breaks

*

Match 0 or more of the preceding token

$

End of the string


Practical case

In this example we will use Filter by Regular Expression to exclude everything that contains the string server in the Type column.

regex-examples-8

Click on the filter icon  regex-Filt-Button regex-Filt-Button  and select Filter By Regular Expression.

filter-by-regular-expression

regular-expression-to-be-used

Having used the expression ^((?!.*server.*).)*$ to filter the column, all lines that contained the string server have now been excluded.

regex-examples-11

TIP

Excluding multiple strings:

If you need to exclude two or more strings, like server and person, you can use the following Regex:

^((?!.*(server|person).*).)*$

^

Beginning of the string

()

Capturing Group

(?!)

Negative look ahead. Specifies a group that cannot match after the main expression.
(i.e. if it matches, then the result is discarded)

.

Matches any character except line breaks

*

Match 0 or more of the preceding token

server

String

|

Match either the expression before or after

person

String

$

End of the string


Using the following Regex gives the same result.

^((?!.*server.*)(?!.*person.*).)*$