The StoreMatchesFromRegExPattern action fetches all the matches of a regular expression pattern from an InputString and stores the array of all the matches into the specified key. The key value can be retrieved using the key name.
Regular expression patterns can be specified as a first parameter to fetch the required data from an input string. Take a look at some of the following scenarios to get better understanding about how this action works.
Scenario 1:
Objective: Matching the beginning of the line.
Sign to be used to define Regular expression: ^
In this case, if user sets the regular expression as ^Qualitia, action will match the lines stating starting with the word Qualitia and stores them into the specified key.
Scenario 2:
Objective:Matching Matching the end ending of the line.
Sign to be used to define Regular expression: $
In this case, if user sets the regular expression as Qualitia$, action will match the lines ending with the word Qualitia and stores them into the specified key.
Scenario 3:
Objective:Matching Matching production, test, or demo instances.
Consider a scenario of a website example.com. This website is configured differently for production, test and demonstration environments. In this case, user needs to set the first variable as a regular expression as mentioned below:
(prod|test|demo)\.example\.com
Assumption: example.com does not change.
In case the input string does not match with regular expression, appropriate message is displayed in the Qualitia test execution report.
Scenario 4:
Objective:Fetches Fetches the email address that is matching to the specified regular expression pattern.
In this case, if user sets the regular expression to find an email address with specific pattern, action will fetch such email addresses and stores under the specified key.
Regular Expression: ^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$
The pattern building is similar to PERL. The following section, explains how to create a pattern based on various factors.
Position Matching
The significance of position matching is to ensure that the regular expressions are placed correctly.
Symbol | Description |
---|---|
^ | Matches beginning of a string |
$ | Matches ending of a string |
\b | Matches any word boundary |
\B | Matches any non-word boundary |
Literals Matching
In this case, any form of characters can be treated as a literal.
For example, alphabet, number, special characters, or even decimal, hexadecimal. Since few of the characters have already got a some special meaning in the context of regular expression, you may need to escape them using escape sequences.
Symbol | Description |
---|---|
Alphanumeric | Matches alphabetical and numerical characters only. |
\n | Matches a new line. |
[ | Matches [ literal only |
] | Matches ] literal only |
( | Matches ( literal only |
) | Matches ) literal only |
\t | Matches horizontal tab |
\v | Matches vertical tab |
| | Matches | literal only |
{ | Matches { literal only |
} | Matches } literal only |
\ | Matches \ literal only |
? | Matches ? literal only |
* | Matches * literal only |
+ | Matches + literal only |
\. | Matches . literal only |
\b | Matches any word boundary |
\B | Matches any non-word boundary |
\f | Matches a form feed |
\r | Matches carriage return |
\ccc | Matches the ASCII character of an octal number ccc. |
\xdd | Matches the ASCII character of an hexadecimal number dd. |
\ucccc | Matches the ASCII character of an UNICODE literal cccc. |
Character Classes Matching
The patterns formed by customized grouping and enclosed within square braces [ ] are called character classes.
If we are expecting a character class that should not be in the list, then we should ignore that particular character class using negative symbol. The negative symbol is caret ^.
Symbol | Description |
---|---|
[xyz] | Match any of the character class enclosed within the character set. |
[^xyz] | Matches any of the character class that are NOT enclosed within the character set. |
. | Matches any character class except \n |
\w | Match any word character class. Equivalent to [a-zA-Z_0-9] |
\W | Match any non-word character class. Equivalent to [^a-zA-Z_0-9] |
\d | Match any digit class. Equivalent to [0-9]. |
\D | Match any non-digit character class. Equivalent to [^0-9]. |
\s | Match any space character class. Equivalent to [ \t\r\n\v\f] |
\S | Match any space character class. Equivalent to [^\t\r\n\v\f] |
Repetition Matching
Repetition matching allows multiple searches within regular expression. It also specifies the number of times an element is repeated in a regular expression.
Symbol | Description |
---|---|
* | Matches 0 or more occurrences of the given regular expression. Equivalent to {0,}. |
+ | Matches one or more occurrences of the given regular Expression. Equivalent to {1,}. |
? | Matches 0 or 1 occurrences of the given regular expression. Equivalent to {0,1}. |
{x} | Matches exactly x number of occurrences of the specified regular expression. |
{x,} | Matches at least x or more occurrences of the given regular expression. |
{x,y} | Matches x to y number of occurrences of the given regular expression. |
Alternation & Grouping
Alternation and grouping helps creating more complex regular expressions in particularly handling intricate clauses within a regular expression offering great flexibility and control.
Symbol | Description |
---|---|
0 | Grouping a clause to create a clause. "(xy)?(z)" matches "xyz" or "z". |
| | Alternation combines one regular expression clause and then matches any of the individual clauses. "(ij)|(23)|(pq)" matches "ij" or "23" or "pq". |
Building Regular Expressions
Here are some examples that clearly explain how to build a regular expression.
Regular Expression | Description |
---|---|
"^\s*.." and "..\s*$" | Represents the possibility of any number of leading and trailing space characters in a single line. |
"((\$\s?)|(#\s?))?" | Represents an optional $ or # sign followed by an optional space. |
"((\d+(\.(\d\d)?)?))" | Represents that at least one digit is present followed by optional decimals and two digits after decimals. |