Using RegEx (Regular Expressions) in Jmeter – CoRelation in Jmeter

RegularExpression

In this article I will describe how to use the Regular expression extractor in Jmeter for the dynamic data corelation needs of web requests. I will first discuss how to add a RegEx to a webrequest, the basics terminologies associated with RegEx and then describe how different types of regular expression requirements can be met.

Lets look at this web request –

As we can see that an id generates for BS_Type parameter of the request. This id changes in every replay and requires parameterization for effective script replay and business flow completion.

To overcome the challenge, we add a regular expression extractor to extract the value of dynamic data during replay and use it in subsequent request. Regular exp extractor can be added by, Right Click on Webrequest -> Click ‘Add’ -> Click ‘Post Processors’ -> Click ‘Regular Expression Extractor’.

Some of the key elements of RegEx are –

  • Reference Name – Name of the variable used for the dynamic id, here it is named as QATypeID. In subsequent requests in JMeter, we’ll reference this variable as ${QATypeId}.
  • Regular Expression – This is the key statement and we will extract the data we’re looking for using this expression. Brackets are put around the value we want to extract. So in our example we have qatype=”(.+?)”     var sr_f = “mn_newsmartsearch.php”; or, Here we are looking for the value that is located between  qatype=” and ”    var sr_f = “mn_newsmartsearch.php”; The expression will search for the strings and identify the right string to parameterize based on values of following elements.
  • Template – Multiple occurance of LB and RB mentioned above is possible and thus multiple values could be extracted. In our case we have just one, and so we’re using the first template,
  • Match No. – In case of multiple matches, this element plays a key role. In our case, we want the first match, so we have mentioned ‘1’.
  • Default value – If no match for mentioned LB and RB combination is found, then one can parse a default value to the variable using this element. It can also be left blank.

Reg Ex text extraction requirements can be different in different scenarios. At times only one value (out of many available) may be required and at times multiple values. The below pasted solutions can be used to meet to different types of text / string requirements.

  • *? – Match 0 or more times
  • +? – Match 1 or more times
  • ?? – Match 0 or 1 time
  • {n}? – Match exactly n times
  • {n,}? – Match at least n times
  • {n,m}? – Match at least n but not more than m times

One can look at the below pasted perl script regex features that supported in Jmeter –

  • (?#text) – An embedded comment that causes text to be ignored.
  • (?:regexp) – Groups things like “()” but doesn’t cause the group match to be saved.
  • (?=regexp) – A zero-width positive lookahead assertion. For example, \w+(?=\s) matches a word followed by whitespace, without including whitespace in the MatchResult.
  • (?!regexp) – A zero-width negative lookahead assertion. For example foo(?!bar) matches any occurrence of “foo” that isn’t followed by “bar”. Remember that this is a zero-width assertion, which means that a(?!b)d will match ad because a is followed by a character that is not b (the d) and a d follows the zero-width assertion.
  • (?imsx) – One or more embedded pattern-match modifier; ‘i’ enables case insensitivity, ‘m’ enables multiline treatment of the input, ‘s’ enables single line treatment of the input, and ‘x’ enables extended whitespace comments.

One thought on “Using RegEx (Regular Expressions) in Jmeter – CoRelation in Jmeter

Leave a Reply

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