A regular expression, or regex, is a sequence of characters that forms a search pattern. This pattern finds other strings.This article explains why you use regexes, provides an overview of their syntax, and demonstrates how you apply them in Ranorex Studio.
How regexes work
Regexes act as search patterns or filter mechanisms within Ranorex Studio.
Ranorex Studio uses regexes in the following areas:
- RanoreXPath expressions: Identify UI elements using patterns.
- Validation actions: Validate strings against a pattern.
- Get actions: Extract strings.
- Weight rule conditions: Define the string that a condition requires in a RanoreXPath expression.
Example: String validation
Consider a validation that checks the value of a UI element attribute. The string contains letters and a number, but only the number matters. The number may change as a result of interacting with the AUT. Therefore, the validation must be able to find any number in the string to be robust. A regex accomplishes this:
- The AUT stores the number of database entries within an alphanumerical string.
- The regex filters the string to find and return only the number.
- The process passes only the number to the validation and ignores the rest of the string.
Regex style
When you create formulas or filters, you can often choose from several different regexes to achieve the same result. Your choice depends on two factors:
Specific vs. general phrasing
In most cases, phrase regexes generally so they apply to more cases. Use specific phrasing only for very specific tasks.
- Example: Dynamic IDs often consist of a prefix or suffix and a random string. Design regexes for these IDs to be as general as possible so they catch multiple patterns rather than just one specific instance.
Detailed vs. minimalistic phrasing
Mathematical principles allow you to phrase regexes minimalistically with very few characters. While economical, this often makes them harder for teammates to read. Keep readability in mind when you work in a team.