This page demonstrates two practical ways to apply regexes in Ranorex Studio.The first example is a validation, and the second example extracts a value with a Get value action.
You need the Demo App for the examples this page covers. If you don’t already have the app on your computer, download it and unpack it to a folder of your choice.
Download the sample solution containing the examples on this page or follow the instructions below to build the solution yourself.
Theme: Regexes applied in Ranorex Studio
Time: Less than 30 min
Instructions
- Unzip the project directory to any folder on your computer
- Start Ranorex Studio and open the solution file
RegEx.rxsln
Validation example
Regexes help validate values that change as you interact with the AUT. In this example, you validate a dynamically generated image ID displayed next to an image. The ID changes every time you hide and show the image.
Solution
A simple text-based validation fails because the ID changes dynamically. A regex handles this change easily.
Configure the validation in Ranorex Studio as follows:
- Select AttributeRegex as the validator operator, as the validation uses a regex.
- The Match name depends on the repository item linked to the action. In this case, validate the Text attribute of the repository item.
- (img_)[0-9a-z]{16}: The regex matches a string that starts with “img_” followed by a 16-digit string consisting of any letters or numbers – this is what the dynamic ID consists of.
- Result: The validation succeeds whenever the Text attribute of the linked repository item has a value that the defined regex matches.
Extract information example
The Get value action extracts attribute values. Use a regex when you require only a specific portion of a value, such as a number.
- Start the Demo App and click the tab Test database.
- Enter the data for a person and add it to the database.
Suppose we now want to extract a person’s age from the resulting string in the database and assign it to a variable.
- The database entry string contains the person’s age.
- In Ranorex Studio, this string is represented by the repository item TextLine. The RanoreXPath expression for this repository item uses the attribute accessiblerole with the value ListItem.
Solution
Use a regex to isolate the age from the full string and assign it to a variable.
Set up the Get value action in Ranorex Studio as follows:
- The entire action in Ranorex Studio.
- Action type Get value to extract a value.
- The linked repository item’s attribute that contains the value.
- The variable that the application assigns the value to.
- [0-9]{1,2}: The regex to extract only the age. In this case, it matches any number with at least one and at most 2 digits, for example, all numbers from 0 to 99.
- Repository item linked to the actions.
Result: If the Text attribute of the linked repository item contains a number from 0–99, Ranorex Studio extracts that number and assigns it to the $myAge variable.
The report displays the successful extraction and assignment: