Welcome to the new Ranorex Support Portal

Regex syntax

On this page, we list the basic regex metacharacters and illustrate the basic syntax of regexes by way of example strings.

Ranorex Studio uses the .NET regex engine. It is quite powerful and complex, and explaining all of it would be beyond the scope of this user guide. If you would like in-depth information on it, please refer to Microsoft’s official .NET guide article.

In this chapter

Metacharacters


There is a set of characters that are used in regexes as operators. These characters are called metacharacters.

If you don’t want the regex engine to interpret them as metacharacter, e.g. because you want to find one in a string, you need to “escape” them. This means preceding the metacharacter with a backslash , i.e. $.

.The period is a placeholder for any single character
^Matches the start of a string
$Matches the end of a string
|Functions as an either-or operator
?Matches the preceding element zero or one time
+Matches the preceding element one or more times
*Matches the preceding element zero or more times
{ }Matches the preceding element the specified number of times
( )Defines a subexpression
[ ]Defines a bracket expression that may contain a set of characters that other metacharacters can be applied to

Examples


Single-element expressions

[1234567]

  • Matches a single character that is contained in the bracket expression, i.e. a number between 1 and 7 here
  • Alternative expression: [1-7]

[Max]

  • Matches any single letter contained in the bracket expression, i.e. “M”, “a”, or “x”.
  • Important: Does not match the word “Max”.

[1-35-8]

  • Matches the numbers 1, 2, 3, 5, 6, 7, 8.
  • Does not match the number 35.

Multi-element expressions

[1-9][ab]

  • Matches any combination of the bracket expressions, i.e. “1a”, “1b”, “2a”, “2b”, …., “9a”, “9b”.

Quantifiers

The quantifier metacharacters ?, +, *, and {} are placed after the characters they should apply to.

[1-9]?

  • Matches a string that contains any of the numbers between 1 and 9 either zero or one time.
  • (Colou?r) would therefore match both “Colour” and “Color”.

[1-9]+

  • Matches any number that consists of one or more numbers between 1 and 9.
  • In other words, matches any number greater than 0.

[0-9]{5}

  • Matches any five-digit number.

[0-9]{3,}

  • Matches any number with at least three digits.

[0-9]{3,5}

  • Matches any number with at least three and at most five digits.

Match beginning of string

^Image_

  • Matches all strings that start with “Image_”

(^Image_)[0-9]{3}

  • Matches all strings that start with “Image_” followed by a three-digit number.
  • Examples for matched strings: “Image_001”, “Image_999”, “Image_127”, …

(^Image_)[0-9]{3}(.jpg)

  • Matches all strings that start with “Image_” followed by a three-digit number and “.jpg”.
  • Note that the period character . is escaped with a backslash .

Match end of string

Sample$

  • Matches all strings that end in “Sample”.

(Sample[0-9]{3}$)

  • Matches all strings that end in “Sample” followed by a three-digit number.

Placeholders

Image.*

  • Matches all strings that consist of “Image” followed by any number of other characters.
  • . is the placeholder for any character.
  • Examples for matched strings: “Image3459834059346237832jkhdsdb”, “Image”, “ImageTheCat”

Match alternatives

((G|g)r(a|e)y)

  • Matches the strings “Gray”, “gray”, “Grey”, and “grey”.

Exclude characters

With [^ you can exclude characters.

[^0-9]

  • Excludes all numbers. Therefore, matches any string that doesn’t contain any numbers.

RanoreXPath examples


You can also use regexes in RanoreXPath expressions. Here are some examples:

ExampleDescription
button[@text~’sample[0-9]’]Matches the following button elements: “sample0”, “sample1”, … “sample9”, “My sample26”.
listitem[@text~’^sample’]Matches all elements whose text attribute value starts with “sample”.
listitem[@text~’sample$’]Matches all elements whose text attribute value ends in “sample”.
listitem[@text~’gr(a|e)y’]Matches all elements whose text attribute value is either “gray” or “grey”.
listitem[@text~’^sample 123$’]Matches all elements whose text attribute value is “sample 123” (use a backslash to escape special characters like space)

USERS LOVE US

SUBSCRIBE TO OUR NEWSLETTER

Get the latest test automation information right in your mailbox.

FOLLOW US

© 2024 Ranorex GmbH. All Rights Reserved