Table of Contents  MOBOTIX Online Help

Regular Expressions

Table of Contents
Regular Expressions
Special Characters

This page should only give you a short introduction into the concept of regular expressions and is far from complete. For more information on the subject, reading Regular Expressions by Jeffrey E.F. Friedl, O'REILLY, is highly recommended.

Regular expressions are search patterns which you can use to test strings. For example, you can use a regular expression to test the message text of an IP message or data sent via the serial interface. A regular expression consists of one or several characters you are searching for and of special characters that are performing certain functions.

Example 1. A machine sends data via the serial interface
[...]
2003-09-22 12:24:50 roboctrl ready
2003-09-22 12:24:51 roboctrl starting process
2003-09-22 12:25:25 roboctrl finished in 34sec
2003-09-22 12:25:30 roboctrl ready
2003-09-22 12:25:31 roboctrl starting process
2003-09-22 12:25:33 error: no material found
2003-09-22 12:25:33 roboctrl stop on error

The regular expression stop.*error will trigger an event if the machine returns an error as in the last line of the example.

Special Characters

If you want to search for a character from the left column of the table below or for the backslash "\" or minus "-" characters, precede them with a backslash "\" (this is also called masking).

Character

Function

^

The ^ character marks the start of a string. The expression ^Test is only true for strings that start with Test.

$

The $ character marks the end of a string. The expression Test$ only is true for strings that end with Test.

.

The full stop character is a placeholder for one character. The ab.d expression is true for abcd or abad, but not for abccd.

When searching for the . character itself, you need to mask it by preceding it with a backslash "\". In this case, the expression ab\.d is only true for ab.d.

*

The asterisk character is a repeat operator meaning that the preceding character may either occur never or an unlimited number of times. The expression ab*c is true for abc, abbbbbc, but also for ac.

+

The plus character is a similar repeat operator meaning that the preceding character may either occur once or an unlimited number of times. The expression ab+c is true for abc, abbbbbc, but not for ac.

?

The question mark character means that the preceding character may occur once or never. The expression ab?c is true for abc and ac, but not for abbc.

[ ]

Expressions surrounded by [square brackets] represent a character class.

A character class can be defined either as a list or as a range. The expression [abc] represents a list of characters, while the expression [a-m] represents a range.

For example, [0-9] is true for all numbers, while [a-z] is true for all lower-case letters. It is possible to use several ranges within one character class, such as [0-9a-zA-Z], or you can mix lists and ranges as in [afm0-6].

A character class can also be negated by using a preceding caret "^" character. The expression [^ab] is true for all characters that are neither a nor b.


© 2001-2024 MOBOTIX · https://www.mobotix.com