Regular Expression (RegEx) – Easy to learn

Regular Expression (RegEx) is a sequence of characters that define a search pattern. Each character in a regular expression is understood to be a metacharacter (with its special meaning), or a regular character (with its literal meaning). For example, if we define a regular expression r. where ‘r’ is a literal character which matches just ‘r’ and ‘.’ is a meta-character that matches every character except a newline. 
In JMeter, Regular Expression plays an important role. Regular Expression is used to identify dynamic values that come in a response. In Microfocus LoadRunner, dynamic values are captured by Correlation whereas, in JMeter, these values are extracted by Regular Expression Extractor

Regular Expression is a pattern that is used to specify a set of strings required for getting dynamic value. To prepare a regular expression, it is important to learn how we can create it using metacharacter and literal character.

The most important and frequently used Reg-Ex for JMeter is (.+?)

The below table will help you to understand how reg-ex can be created. The blue coloured text is the extracted value through the related token.

TokensMeaningExample
.Matches any character other than newlineStatement: Regular Expression is a pattern

Token: .

Extracted value: Regular Expression is a pattern
.+Matches any string excluding new lineStatement: Regular Expression is a pattern

Token: .+

Extracted value: Regular Expression is a pattern
.*Matches any string including a new lineStatement: Regular Expression is a pattern

Token: .*

Extracted value: Regular Expression is a pattern
X.*YMatches as many characters as possible.Statement: Regular Expression is a pattern

Token: E.*i

Extracted value: Regular Expression is a pattern
(.+?)Matches the strings including letters, digits and space (exclude new line)Statement: Regular Expression is a pattern

Token: Reg(.+?)pattern

Extracted value: Regular Expression is a pattern
()Matches everything or enclosed character/stringStatement: Regular Expression is a pattern

Token: ()

Extracted value: Regular Expression is a pattern
OR

Statement: Regular Expression is a pattern

Token: (press)

Extracted value: Regular Expression is a pattern
|OR operationStatement: Regular Expression is a pattern

Token: (Reg|Ex)

Extracted value: Regular Expression is a pattern
a?Matches an `a` character or nothingStatement: Regular Expression is a pattern

Token: a?

Extracted value: Regular Expression is a pattern
a*Matches zero or more consecutive `a` charactersStatement: Regular Expression is a pattern

Token: a*

Extracted value: Regular Expression is a pattern
a+Matches one or more consecutive `a` characters.Statement: Regular Expression is a pattern

Token: a+

Extracted value: Regular Expression is a pattern
{n}Matches exactly n consecutive charactersStatement: Regular Expression is a pattern

Token: s{2}

Extracted value: Regular Expression is a pattern
{n,}Matches at least n consecutive charactersStatement: Regular Expression is a pattern. s ss sss ssss sssss ssssss.

Token: s{3}

Extracted value: Regular Expression is a pattern. s ss sss ssss sssss ssssss.
{m,n}Matches between m and n (inclusive) consecutive charactersStatement: Regular Expression is a pattern. s ss sss ssss sssss ssssss.

Token: s{2,4}

Extracted value: Regular Expression is a pattern. s ss sss ssss sssss ssssss
\*Escape CharacterStatement: Regular Expression is a pattern

Token: \\*s

Extracted value: Regular Expression is a pattern
[]Matches specific character (case sensitive)Statement: Regular Expression is a pattern

Token: [a]

Extracted value: Regular Expression is a pattern
or

Statement: Regular Expression is a pattern

Token: [aer]

Extracted value: Regular Expression is a pattern
[^]Matches any character except the given characterStatement: Regular Expression is a pattern

Token: [^a]

Extracted value: Regular Expression is pattern
or 

Statement: Regular Expression is a pattern

Token: [^ae]

Extracted value: Regular Expression is pattern
[a-z]Matches any characters between a and zStatement: Regular Expression is a pattern

Token: [a-z]

Extracted value: Regular Expression is a pattern
[^a-z]Matches any characters except those in the range a-z, even space also.Statement: Regular Expression is a pattern

Token: [^a-z]

Extracted value: Regular Expression is a pattern
[A-Z]Matches any characters between A and ZStatement: Regular Expression is a pattern

Token: [A-Z]

Extracted value: Regular Expression is a pattern
[^A-Z]Matches any characters except those in the range A-Z, even space also.Statement: Regular Expression is a pattern

Token: [^A-Z]

Extracted value: Regular Expression is a pattern
[a-zA-Z]Matches any characters between a-z or A-ZStatement: Regular Expression is a 123 pattern

Token: [a-zA-Z]

Extracted value: Regular Expression is a 123 pattern
[^a-zA-Z]Matches any characters between a-z or A-ZStatement: Regular Expression is a 123 pattern

Token: [^a-zA-Z]

Extracted value: Regular Expression is a 123 pattern
\This is used to match the literal value of any metacharacterStatement: Regular Expression is a pattern

Token: \a

Extracted value: Regular Expression is a pattern
\sMatches any space, tab or newline characterStatement: Regular Expression is a pattern

Token: \s

Extracted value: Regular_Expression_is_a_pattern
*It will capture all the spaces which are represented as _
\SMatches any character other than space, tab or newline.Statement: Regular Expression is a pattern

Token: \S

Extracted value: Regular_Expression_is_a_pattern
*It will not capture any space. Just for representation purpose, they are marked as _.
\S+Matches any string other than a space, tab or newline.Statement: Regular Expression is a pattern

Token: \S+

Extracted value: Regular_Expression_is_a_pattern
*It will not capture any space. Just for representation purpose, they are marked as _.
\d or [0-9]Matches any decimal digitStatement: Regular Expression is a 123 pattern

Token: \d+

Extracted value: Regular Expression is a 123 pattern
or

Statement: Regular Expression is a 123 pattern

Token: [0-9]

Extracted value: Regular Expression is a 123 pattern
\DMatches any character other than a decimal digitStatement: Regular Expression is a 123 pattern

Token: \D

Extracted value: Regular Expression is a 123 pattern
\D+Matches any string other than a decimal digitStatement: Regular Expression is a 123 pattern

Token: \D+

Extracted value: Regular Expression is a 123 pattern
\w or [a-zA-z0-9_]Matches any letter, digit or underscore (exclude white space)Statement: Regular Expression is a 123_321 pattern

Token: \w

Extracted value: Regular Expression is a 123_321 pattern
*It will not capture any space.
\W or [^a-zA-Z0-9_]Matches anything other than a letter, digit or underscoreStatement: Regular Expression is a pattern

Token: \W

Extracted value: Regular Expression is a 123_321 pattern
*It will capture all the spaces.
\vMatches newlines and vertical tabs
\nMatches a newline character
\rMatches a carriage return character
\tMatches a tab character
\0Matches a null character
[\b]Matches the backspace control character.
(.)\Usually referred to as a `backreference`, this will match a repeat of the text captured in a previous set of parenthesesStatement: Regular Expression is a pattern

Token: (.)\a

Extracted value: Regular Expression is a pattern
Matched: ‘la’, ‘ a’ and ‘pa’
\uYYYYMatches the Unicode character with the given hex value
\xYYMatches the 8-bit character with the given hex value
\dddMatches the 8-bit character with the given octal value.
\cYMatches ASCII characters

You can build Reg-Ex using online tools available in the market. I have provided some useful Regular Expression Extractors/Testers links below:

  1. https://extendsclass.com/regex-tester.html 
  2. http://www.regexpal.com
  3. http://regexr.com
  4. http://myregexp.com
  5. http://www.freeformatter.com/regex-tester.html
  6. http://www.regextester.com
  7. https://regex101.com
  8. http://www.regexplanet.com/advanced/java/index.html


Leave a Reply

Your email address will not be published. Required fields are marked *