Regex is a powerful tool to search, split or to replace operations on strings. LEAPWORK has a Regular Expression building block that allows you to incorporate regex into you test flow.
This article will look at 3 different examples:
- Match: Check if an input string matches a specified string pattern.
- Split: Split an input string into a list of elements based on a regex and iterate the results.
- Replace: Use the matching sub string in an input string to replace in the output string.
Regular Expression building block
When you insert a Regular expression building block, you add the input text at the "Text" field. You can either specify directly in the field or connect a blue wire from another building block.
The "Expression" field holds the actual regex and the Method dropdown specifies the regex method (Match, Split or Replace).
If a match is found for any of the methods, the green connector at the top will be triggered. Otherwise the "Not found" connector will be triggered. You can use this to branch your flow.
The "Use occur." allows you to specify which occurrence to use in case you have multiple matches. As with other similar building blocks in LEAPWORK you can specify the exact occurrence to use or select "All" to iterate all matching result. If you select "All" then the top green connector will be triggered for each found element.
Match
When you select the match method, the entire input string is matched against the rule/pattern outlined in the Expression field. In the example below the expression is verifying if the input string is an email address or not.
Input: kasperfehrend@leaptest.com
Expression: ^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$
In this case the input text matches the regex, which triggers the top connector. The Log Message will output the match - in this case the full email specified in the Text field.
Split
When you use the Split method, the input string is split up using the pattern specified in the expression as the delimiter.
In the example shown below, the expression (delimiter) is "\W" which means a "non-character" - in this case a "space". The "Method" is set to "Split" and the "Use occur" is set to "All". This means the building block iterate every word (delimited by a space) in the input text, and trigger the green top connector for every word. The "Result" for each iteration is added as input to a log message as well as the "Current index".
Input: Bob and Michelle are from Indiana
Expression: \W
Output:
1: Bob
2: and
3: Michelle
4: are
5: from
6: Indiana
Replace
Using the "Replace" method is like performing a search-replace on a string. You will define 2 expressions: one to match a part of the input string, and one to insert the matched sub string into.
In the example below the input string is a URL (https://leaptest.com) and the matching expression will match any string after the term "http://". The replacement expression will use the "$&" as a token to insert the value found in the matching expression. So in other words, "www.leaptest.com" will be extracted from the input string and inserted into the "<a>"-tag.
Input: http://www.leaptest.com
Expression (match): http://\S+
Expression (replace): <a href="$&">$&</a>
Output: <a href="http://www.leaptest.com">http://www.leaptest.com</a>