Pages

Friday 28 April 2017

Cheat Sheet for Regular Expression in Jmeter

Apache Jmeter, Regular Expression, RegEx
In Jmeter, Regular Expression play very important role. It is used to identify dynamic values in response which is coming from server. Regular expression captures dynamic value coming from server matching left and right boundaries. 

There are multiple types of regular expression we need to apply on day to day basis in our scripting. This blog will cover all types of regular expression with Example.


1. (\d+) and (\d) and [0-9]+


 It is used to capture decimal dynamic values coming from server. If you want extract just one digit from dynamic values (\d) can be used. if you want to extract multiple decimals in a one go (\d+) is used


Example:


a. <decimal-Values>123456</decimal-Values>


Regular expression to capture 123456: 


<decimal-Values>(\d+)</decimal-Values>


or


<decimal-Values>(\d\d\d\d\d\d)</decimal-Values>


or


<decimal-Values>([0-9]+)</decimal-Values>


b. <decimal-Values>5</decimal-Values>


Regular expression to capture 5:


<decimal-Values>(\d)</decimal-Values>


2. (\D+) and (\D)


It is used to capture all dynamic value coming from server except decimal. It capture alphabets, White Space, New lines, Special Character. 


Example:


a. <Random-Values>a$%^sdg @ASzG<Random-Values>


Regular expression to capture a$%^sdg @ASzG:


<Random-Values>(\D+)<Random-Values>


b. <Random-Values>@<Random-Values>


Regular expression to capture @:


<Random-Values>(\D)<Random-Values>


3. (\w+) and (\w)


It is used to capture alphabets coming from server in capital or small letter. (\w+) is used for capturing multiple alphabets and underscore and (\w) used for capturing single alphabet.


Example:


a. <Sample-Words>abcdEFGH<\Sample-Words>


Regular Expression for capturing abcdEFGH:


<Sample-Words>(\w+)<\Sample-Words>


b. <Sample-Word>H<\Sample-Word>


Regular Expression for capturing H:


<Sample-Words>(\w)<\Sample-Words>


4. (\W+) and (\W)


It is used to extract all dynamic data except alphanumeric. It extract white space as well Special characters except underscore. 


Example:


a. <Target-Value>$@#$@  $#^*</Target-Value>


Regular Expression for extracting $@#$@  $#^*:


<Target-Value>(\W+)</Target-Value>


b. <Target-Value>$</Target-Value>


Regular Expression for extracting $:


<Target-Value>(\W)</Target-Value>


5. (\s) and (\s+)


it is used to capture white space from dynamic data coming from server. We generally don't need to capture white space as a Regular expression but it can be used in combine with other operations like (\d+) or (\w+). For more details see below example.


Example:


a. <Target-data>1234567   abcdefgh</Target-data>


Regular expression for capturing 1234567 and abcdegfh separately when No of white space is not fixed (i.e. sometimes there is 2 white space between strings sometimes 3 white space and sometimes more than 3):


 <Target-data>(\d+)\s+(\w+)</Target-data>


Note: we have not given \s+ in bracket as we don't want to capture it in variable, but it is working as a supportive Regular expression to capture Numeric and Alphabets 


6 (\S+) and (\S)


It is used to capture all the dynamic data except White Space. 


Example:


a. <Target-data>12ad@_!@#$%^&*()</Target-data>


Regular Expression to Capture 12ad@_!@#$%^&*():


<Target-data>(\S+)</Target-data>


7.[a-z]+ and [A-Z]+


It is used to extract any alphabets between a-z. [a-z] is used to extract only small letter. [A-Z] is used to extract only capital letter. Both can be used together to extract mixed letter.


Example:


a. <letter>performanceoptimize</letter>


Regular expression to extract performanceoptimize:


<letter>([a-z]+)</letter>


b. <letter>PERFORMANCEOPTIMIZE</letter>


Regular expression to extract PERFORMANCEOPTIMIZE:


<letter>([A-Z]+)</letter>


c. <letter>PerForManCeoPtiMizE</letter>


Regular expression to extract PerForManCeoPtiMizE:


<letter>([a-zA-Z]+)</letter>


d. if you want to find specific range of characters like between c to j that can also be done using range of c-j. it applies to both capital and small letter


<letter>cdefj</letter>


Regular expression to extract cdefj:


<letter>([c-j]+)</letter>


e. if you want to find only specific character without specifying any range, it is possible as shown below


<letter>agfefhaaeeffg<\letter>


Regular Expression to extract agfefhaaeeffg:


<letter>([aefgh]+)</letter>


8. [^a-z]+ and [^A-Z]+


it is used to match any character, decimal, special character and White space other than mentioned inside box bracket. i.e. [^a-z] will match any character, decimal, special character and white space other than a-z alphabets or [^A-Z] will match any character, decimal, special characters and white space other than A-Z alphabets. 


Example:


a. <letter>1234!@#$%^&*()_-++==</letter>


Regular Expression to extract 1234!@#$%^&*()_-++==:


<letter>([^a-z]+)</letter>


b. <letter>abc1234!@#$%^&*()_-++</letter>


Regular Expression to extract abc1234!@#$%^&*()_-++:


<letter>([^A-Z]+)</letter>


9.[a-z0-9@#$]+ and such Regular expression


If you want to match very specific set of alphabetes, special characters or numeric then you can use this custom facility for it.


Example:


a. If you want to extract tokenId and you know that it will have capital letter, small letter and 0-9 numbers and it won't have any special character or any white space. Then you can use following Custom Regular Expression.


<tokenid>ArtrDeR5HGHDDSsdccasds2342sdscDF</tokenid>


Regular Expression to extract ArtrDeR5HGHDDSsdccasds2342sdscDF:


<tokenid>([a-zA-z0-9]+)</tokenid>


b. if you just want to extract only special character with white space that can also be handled with below custom method


<tokenid>!@#$%^&*   (#@$#$@#$%#)_+_</tokenid>


Regular Expression to extract !@#$%^&*   (#@$#$@#$%#)_+_:


<tokenid>([!@#$%^&*() _+]+)</tokenid>


10. (.*?) and (.+?)


It is used to extract alphanumeric, Special character and white space. Generally you should avoid using both of this in regular expression as it can capture everything. 


Example:


a. <test>asdas!@##sdad@#@`211231adsdSAWSSAD</test>


Regular Expression to capture asdas!@##sdad@#@`211231adsdSAWSSAD: 

<test>(.+?)</test>


11. there are other different type of Regular expression but it is not widely used. you can see below brief about it



Tokens
Meaning
Example
a+
Matches one or more consecutive a character
<test>aaaaaaaa</test>

RegEx to find aaaaaaaa:

<test>(a+)</test>

a*
Matches zero or more consecutive a character
<test>aaaaaaaa</test>

RegEx to find aaaaaaaa:

<test>(a*)</test>

a?
Matches an `a` character or nothing
<test>a</test>

RegEx to find aaaaaaaa:

<test>(a?)</test>

a{n}
Matches exactly n consecutive characters
<test>aaaaa</test>

RegEx to find aaaaa:

<test>(a{5})</test>
a{n,}
Matches n or more consucative characters
<test>aaaaa</test>

RegEx to find aaaaa:

<test>(a{2,})</test>
a{m,n}
Matches consecutive characters between m and n

<test>aaaaa</test>

RegEx to find aaaaa:

<test>(a{2,5})</test>

a+?
Matches as few as possible
<test>aaaaa</test>

RegEx to find aaaaa:

<test>(a+?)</test>
ab|cd
Matches either ab or cd 
<test>ab</test>

RegEx to find aaaaa:

<test>(ab|cd)</test>
\n, \t, \r 
\n is for matching new line character, \t is for matching tab and \r is for matching carriage return


Easiest way to build any regular expression is to use available online tool which is very user friendly and allows you to create regular expression more effectively. Personally, I am using http://www.regextester.com/ for building up my regular expression.

You might face more challenge in finding correct and more accurate Regular expression for your work. You can ask me in comment section or in query section about problems you are facing while matching regular expression. 




No comments:

Post a Comment