By utilizing regular expressions, you can check for strings that contain random values and extract only the necessary parts to store into variables.
Table of contents
- List of commonly used regular expressions
- Specific examples of commonly used regular expressions
- How to verify regular expressions
List of commonly used regular expressions
Regular expressions | Example usage | Example of matching strings | |
. | Matches any single character | MagicP.d |
MagicPod MagicPOd |
* | Matches the preceding character zero or more times | MagicPo*d |
MagicPd MagicPooooood |
+ | Matches the preceding character one or more times | MagicPo+d |
MagicPod MagicPooooood |
? | Matches the preceding character zero or one time | MagicPo?d |
MagicPd MagicPod |
^ | Matches the start of a line | ^Magic |
Magic MagicPod |
$ | Matches the end of a line | Pod$ |
Pod MagicPod |
| | Matches either expression | MagicPod|magicpod |
MagicPod magicpod |
() | Groups expressions | Magic(Pod|Dop) |
MagicPod MagicDop |
[] | Matches any of the characters inside the brackets | [MagicPod] | M,a,g,i,c,P,o,d |
[^] | Matches any character not inside the brackets | [^MagicPod] | Except M,a,g,i,c,P,o,d |
{n} | Matches the preceding character exactly n times | MagicPo{3}d | MagicPoood |
{n,} | Matches the preceding character n or more times | MagicPo{3,}d |
MagicPoood MagicPoooood |
{m,n} | Matches the preceding character between m and n times | MagicPo{1,3}d |
MagicPod MagicPood MagicPoood |
\d | Matches a single digit (0-9) | MagicPod Ver\.\d\.0 | MagicPod Ver.1.0 |
\s | Matches a whitespace character | Magic\sPod | Magic Pod |
\n | Matches a newline character | Magic\nPod |
Magic Pod |
Specific examples of commonly used regular expressions
Checking downloaded file names
Using the "Assert download is completed" command, you can check file names that contain random values.
The "Wait until download is completed" and "Store text file contents" commands can also utilize regular expressions.
Example 1: The part after "Data" changes with each execution.
Data.*\.txt
- Matching file names: Data1.txt, Data123.txt, DataABC.txt
Example 2: The part after "Data" captures the date.
Data\d{8}\.txt
- Matching file names: Data20240101.txt
Example 3: The part before "Table" changes with each execution.
.*Table\.csv
- Matching file names: 2024-01-01Table.csv, UserATable.csv
More details here.
Extracting parts that match conditions and storing them in variables
Using the "Store substring matching regular expression" command, you can extract parts that match the conditions.
Example 1: Extract only the URL
https:\/\/example.com\/.+
More details here.
Example 2: Extract only the 6-digit authentication code
[0-9]{6}
More details here.
Example 3: Extract the part before "yen"
(.*)(?=yen)
- Target string: 1,000yen (tax incl.)
- Extracted result: 1,000
Example 4: Extract the part after "yen"
(?<=yen)(.*)
- Target string: 1,000yen (tax incl.)
- Extracted result: (tax incl.)
Example 5: Extract between "the amount is" and "yen"
(?<=the amount is ).*(?=yen)
- Target string: the amount is 1,000yen
- Extracted result: 1,000
Example 6: Extract from "amount is" to "yen"
amount is.*yen
- Target string: User A's amount is 1,000yen in total.
- Extracted result: amount is 1,000yen
Example 7: Extract up to the first decimal place
.*\..
- Target string: 12.345
- Extracted result: 12.3
Example 8: Extract the third character
(?<=^.{2}).{1}
- Target string: 123456
- Extracted result: 3
Example 9: Extract only the numbers
[0-9]+
- Target string: The ID is 12345.
- Extracted result: 12345
Replacing parts that match conditions
Using the "Store the result of the replacement" command, you can replace parts that match the conditions. Choose "Regular expression" for the condition.
Example 1: Remove leading zeros
\b0+
- Target string: 0010
- Replacement result: 10
How to verify regular expressions
You can use JavaScript regular expression syntax. Specifically, the ECMAScript 2021 syntax, which is the JavaScript standard.
To verify your expressions before execution, use a regular expression checker that supports JavaScript syntax.
The following regular expression checkers are compatible with ECMAScript 2018 and later (there are no significant differences between ECMAScript 2018 and ECMAScript 2021):
regex101 (Select "ECMAScript (JavaScript)" in the "FLAVOR" section on the left pane)
WWW CREATORS Regex Checker