<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>PerformanceEngineer.com &#187; Automated testing</title>
	<atom:link href="http://www.performanceengineer.com/blog/category/automated-testing/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.performanceengineer.com/blog</link>
	<description>Software Performance Engineering &#38; Testing</description>
	<lastBuildDate>Sat, 23 Jan 2010 02:02:43 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Examples on LoadRunner Regular Expressions</title>
		<link>http://feeds.feedburner.com/~r/motevich/~3/281415093/examples-on-loadrunner-regular.html</link>
		<comments>http://feeds.feedburner.com/~r/motevich/~3/281415093/examples-on-loadrunner-regular.html#comments</comments>
		<pubDate>Thu, 01 May 2008 07:11:00 +0000</pubDate>
		<dc:creator>noreply@blogger.com (http://motevich.blogspot.com)</dc:creator>
				<category><![CDATA[Automated testing]]></category>
		<category><![CDATA[From The Web]]></category>
		<category><![CDATA[loadrunner]]></category>
		<category><![CDATA[visual tutorials]]></category>

		<guid isPermaLink="false">tag:google.com,2005:reader/item/4fffe87257f4e715</guid>
		<description><![CDATA[I'm going to show and explain <span style="font-weight:bold;color:rgb(0, 0, 153)">how to use Regular Expressions in LoadRunner</span>.<br /><br /><span style="font-weight:bold">Introduction:</span><br />The present article is a summarizing of the <a href="http://motevich.blogspot.com/2008/04/loadrunner-regular-expressions.html" onclick="pageTracker._trackPageview('/outgoing/motevich.blogspot.com/2008/04/loadrunner-regular-expressions.html?referer=');">LoadRunner Regular Expressions challenge</a> and <a href="http://motevich.blogspot.com/2008/04/loadrunner-regexp-challenge-resolved.html" onclick="pageTracker._trackPageview('/outgoing/motevich.blogspot.com/2008/04/loadrunner-regexp-challenge-resolved.html?referer=');">its results</a>. Also, I added code for RegExp patterns/subpatterns matching.<br />All LoadRunner Regular Expressions functions are shown with examples.<br /><br /><span style="font-weight:bold"><br />Outline:</span> <ol style="font-weight:bold;color:rgb(0, 0, 153)"><li>How to check - whether RegExp pattern matches against a text or not<br /></li><li>How to get a matched strings (RegExp patterns and subpatterns)<br /></li></ol><span style="font-weight:bold"></span><div style="text-align:center"><span style="font-weight:bold;color:rgb(0, 0, 153)"><br />How to check - Whether RegExp pattern matches against a text or not</span><span style="color:rgb(0, 0, 0);font-weight:normal"></span><br /><span style="color:rgb(0, 0, 0);font-weight:normal"></span></div><span style="color:rgb(0, 0, 0);font-weight:normal"><br />I thanks Charlie Weiblen and Tim Koopmans for the solution. I modified it slightly.<br />So, here it is:</span> <ol><li><span style="font-weight:bold">Download and unpack</span> <a href="http://gnuwin32.sourceforge.net/downlinks/pcre-bin-zip.php" onclick="pageTracker._trackPageview('/outgoing/gnuwin32.sourceforge.net/downlinks/pcre-bin-zip.php?referer=');">Binaries</a> and <a href="http://gnuwin32.sourceforge.net/downlinks/pcre-lib-zip.php" onclick="pageTracker._trackPageview('/outgoing/gnuwin32.sourceforge.net/downlinks/pcre-lib-zip.php?referer=');">Developer files</a> for PCRE (Perl Compatible Regular Expressions).<br />These and others files are available on <a href="http://gnuwin32.sourceforge.net/packages/pcre.htm" onclick="pageTracker._trackPageview('/outgoing/gnuwin32.sourceforge.net/packages/pcre.htm?referer=');">Pcre for Windows page</a>.<br /><br /></li><li><span style="font-weight:bold">Unzip</span> downloaded archives into c:\pcre<br /><a href="http://bp0.blogger.com/_fjL89jPDk9E/SBly60d-TvI/AAAAAAAAAf8/W1jqTIY4rYE/s1600-h/1_pcre_folder_c.gif" onclick="pageTracker._trackPageview('/outgoing/bp0.blogger.com/_fjL89jPDk9E/SBly60d-TvI/AAAAAAAAAf8/W1jqTIY4rYE/s1600-h/1_pcre_folder_c.gif?referer=');"><img style="margin:0px auto 10px;display:block;text-align:center" src="http://bp0.blogger.com/_fjL89jPDk9E/SBly60d-TvI/AAAAAAAAAf8/W1jqTIY4rYE/s400/1_pcre_folder_c.gif" alt="" border="0"></a></li><li><span style="font-weight:bold">?omment out</span> the include for <span style="color:rgb(0, 0, 153)">stdlib.h</span> file in:<br /><ul><li>C:\pcre\include\pcre.h</li><li>C:\pcre\include\pcreposix.h<br /></li></ul><a href="http://bp0.blogger.com/_fjL89jPDk9E/SBl5P0d-TwI/AAAAAAAAAgE/SB1_lOv75G8/s1600-h/2_commented_out_c.gif" onclick="pageTracker._trackPageview('/outgoing/bp0.blogger.com/_fjL89jPDk9E/SBl5P0d-TwI/AAAAAAAAAgE/SB1_lOv75G8/s1600-h/2_commented_out_c.gif?referer=');"><img style="margin:0px auto 10px;display:block;text-align:center" src="http://bp0.blogger.com/_fjL89jPDk9E/SBl5P0d-TwI/AAAAAAAAAgE/SB1_lOv75G8/s400/2_commented_out_c.gif" alt="" border="0"></a></li><li>In your LoadRunner script, <span style="font-weight:bold">add to globals.h</span>:<br /><ul style="color:rgb(51, 153, 153)"><li>#include "c:\\pcre\\include\\pcre.h"</li><li>#include "c:\\pcre\\include\\pcreposix.h"</li></ul><a href="http://bp0.blogger.com/_fjL89jPDk9E/SBl7R0d-TxI/AAAAAAAAAgM/xqAxe_iXA9A/s1600-h/3_globals_h_c.gif" onclick="pageTracker._trackPageview('/outgoing/bp0.blogger.com/_fjL89jPDk9E/SBl7R0d-TxI/AAAAAAAAAgM/xqAxe_iXA9A/s1600-h/3_globals_h_c.gif?referer=');"><img style="margin:0px auto 10px;display:block;text-align:center" src="http://bp0.blogger.com/_fjL89jPDk9E/SBl7R0d-TxI/AAAAAAAAAgM/xqAxe_iXA9A/s400/3_globals_h_c.gif" alt="" border="0"></a></li><li>Add the <span style="font-weight:bold">match()</span> function to vuser_init section:<br /><div style="border:1px solid gray;margin:10pt;padding:10pt;background-color:rgb(240, 240, 240);font-family:monospace"><span style="color:Green"></span><div><div><font color="Green">//////////////////////////////////////////////////////////////////////////</font> <br /><font color="Green">/// &#39;match&#39; function matches a &#39;pattern&#39; against a given &#39;subject&#39;</font> <br /><font color="Green">/// It returns 1 for a match, or 0 for a non-match / error</font> <br /><font color="Blue">int</font> match(<font color="Blue">const</font> <font color="Blue">char</font> *subject, <font color="Blue">const</font> <font color="Blue">char</font> *pattern) <br />{ <br />    <font color="Blue">int</font> rc;            <font color="Green">// Returned code</font> <br />    regex_t re;        <font color="Green">// Compiled regexp pattern</font> <br />     <br />    lr_load_dll(<font color="Maroon">"c:\\pcre\\bin\\pcre3.dll"</font>); <br />     <br />    <font color="Blue">if</font> (regcomp(&#38;re, pattern, <font color="Maroon">0</font>) != <font color="Maroon">0</font>) <br />        <font color="Blue">return</font> <font color="Maroon">0</font>;     <font color="Green">// Report error</font> <br />     <br />    rc = regexec(&#38;re, subject, <font color="Maroon">0</font>, <font color="Blue">NULL</font>, <font color="Maroon">0</font>); <br />    regfree(&#38;re); <br /> <br />    <font color="Blue">if</font> (rc != <font color="Maroon">0</font>) <br />        <font color="Blue">return</font> <font color="Maroon">0</font>;     <font color="Green">// Report error</font> <br />    <font color="Blue">else</font> <br />        <font color="Blue">return</font> <font color="Maroon">1</font>; <br />}</div><br /></div></div></li><li>Let's <span style="font-weight:bold">run</span> sample LoadRunner script and check the result:<br /><a href="http://bp3.blogger.com/_fjL89jPDk9E/SBmHVkd-TyI/AAAAAAAAAgU/jdl_DCahHT8/s1600-h/4_match_script_c.gif" onclick="pageTracker._trackPageview('/outgoing/bp3.blogger.com/_fjL89jPDk9E/SBmHVkd-TyI/AAAAAAAAAgU/jdl_DCahHT8/s1600-h/4_match_script_c.gif?referer=');"><img style="margin:0px auto 10px;display:block;text-align:center" src="http://bp3.blogger.com/_fjL89jPDk9E/SBmHVkd-TyI/AAAAAAAAAgU/jdl_DCahHT8/s400/4_match_script_c.gif" alt="" border="0"></a>As you can see, <span style="font-weight:bold">match()</span> function works correctly. Using <span style="font-weight:bold">match</span><span style="font-weight:bold">()</span> function, you can check - whether RegExp pattern matches against a text or not.<br /><br />It can be helpful, when you verify in LoadRunner that the text (RegExp pattern) matches the text on a downloaded page.<br /><br />I tested the <span style="font-weight:bold">match</span><span style="font-weight:bold">()</span> function with different patterns and subject strings:<br /><table style="width:590px;height:106px" border="1"><tbody><tr><td style="font-weight:bold;text-align:center;color:rgb(0, 0, 153)">#<br /></td><td style="font-weight:bold;text-align:center;color:rgb(0, 0, 153)">Subject string<br /></td><td style="text-align:center;color:rgb(0, 0, 153)"><span style="font-weight:bold">Patterns</span><br /></td><td style="text-align:center;color:rgb(0, 0, 153)"><span style="font-weight:bold">Result of</span><br /><span style="font-weight:bold">match()</span><br /></td><td style="text-align:center;color:rgb(0, 0, 153)"><span style="font-weight:bold">Is correct</span><br /><span style="font-weight:bold">result?</span><br /></td></tr><tr><td style="text-align:center">1<br /></td><td>abcdef</td><td>b(c(.*))e<br /></td><td style="text-align:center">1<br /></td><td style="text-align:center">Yes<br /></td></tr><tr><td style="vertical-align:top;text-align:center">2<br /></td><td style="vertical-align:top">abcdef<br /></td><td style="vertical-align:top">b(z(.*))e<br /></td><td style="vertical-align:top;text-align:center">0<br /></td><td style="vertical-align:top;text-align:center">Yes<br /></td></tr><tr><td style="vertical-align:top;text-align:center">3<br /></td><td style="vertical-align:top">2008<br /></td><td style="vertical-align:top">\\d{2,5}<br /></td><td style="vertical-align:top;text-align:center">1<br /></td><td style="vertical-align:top;text-align:center">Yes</td></tr><tr><td style="vertical-align:top;text-align:center">4<br /></td><td style="vertical-align:top">2008<br /></td><td style="vertical-align:top">\\d{5}<br /></td><td style="vertical-align:top;text-align:center">0<br /></td><td style="vertical-align:top;text-align:center">Yes</td></tr><tr><td style="vertical-align:top;text-align:center">5<br /></td><td style="vertical-align:top">abc 1st of May 2008xyz<br /></td><td style="vertical-align:top">\\d.*\\d<br /></td><td style="vertical-align:top;text-align:center">1<br /></td><td style="vertical-align:top;text-align:center">Yes<br /></td></tr></tbody></table>Note: Since LoadRunner uses ANSI C language, please do not forget to double backslashes (\\). For example, to match any digit character (0-9), use pattern <span style="font-weight:bold">"\\d"</span>.<br /><br /><span style="font-weight:bold">match()</span> function is simple enough. But it searches only and it cannot <span style="font-weight:bold">extract</span> matched subpatterns from the text. For example, we have to <span style="font-weight:bold;color:rgb(0, 0, 153)">extract the name of month</span> from these strings:<br /><ul style="font-style:italic"><li>"abc 1st of May 2008xyz"</li><li>"abc 25th of February 2031"</li><li>etc<br /></li></ul>We can use the following pattern:<br /><ul><li>\d.+([A-Z]\w+)\s+\d{4}<br /></li></ul><br />The name of month will be matches by subpattern ([A-Z]\w+). <span style="color:rgb(0, 0, 153);font-weight:bold">How to extract the found text?</span> You can use <span style="font-weight:bold">matchex()</span> function for that. Let's discuss it in details...<br /><br /></li></ol><div style="text-align:center"><span style="font-weight:bold;color:rgb(0, 0, 153)">How to get a matched strings (RegExp patterns and subpatterns)</span><br /></div><br />To get a matched (found) strings, we have to update our <span style="font-weight:bold">match() </span> function.<br />That's why I created <span style="font-weight:bold">matchex() </span>('match' EXtended) function.<br /><ol><li>Add the <span style="font-weight:bold">match</span><span style="font-weight:bold">ex()</span> function to vuser_init section<br /><div style="border:1px solid gray;margin:10pt;padding:10pt;background-color:rgb(240, 240, 240);font-family:monospace"><div><font color="Green">//////////////////////////////////////////////////////////////////////////</font> <br /><font color="Green">/// &#39;matchex&#39; (EXtended) function matches a &#39;pattern&#39; against a given &#39;subject&#39;</font> <br /><font color="Green">/// It returns number of matches:</font> <br /><font color="Green">///     0 - for a non-match or error</font> <br /><font color="Green">///     1 and more - for successful matches</font> <br /><font color="Blue">int</font> matchex(<font color="Blue">const</font> <font color="Blue">char</font> *subject, <font color="Blue">const</font> <font color="Blue">char</font> *pattern, <font color="Blue">int</font> nmatch, regmatch_t *pmatch) <br />{ <br />    <font color="Blue">int</font> rc;            <font color="Green">// Returned code</font> <br />    regex_t re;        <font color="Green">// Compiled regexp pattern</font> <br /> <br />    lr_load_dll(<font color="Maroon">"c:\\pcre\\bin\\pcre3.dll"</font>); <br /> <br />    <font color="Blue">if</font> (regcomp(&#38;re, pattern, <font color="Maroon">0</font>) != <font color="Maroon">0</font>) <br />        <font color="Blue">return</font> <font color="Maroon">0</font>;     <font color="Green">// Report error</font> <br /> <br />    rc = regexec(&#38;re, subject, nmatch, pmatch, <font color="Maroon">0</font>); <br />    pcre_free(&#38;re);    <font color="Green">// Release memory used for the compiled pattern</font> <br /> <br />    <font color="Blue">if</font> (rc &#60; <font color="Maroon">0</font>) <br />        <font color="Blue">return</font> <font color="Maroon">0</font>;     <font color="Green">// Report error</font> <br /> <br />    <font color="Green">// Get total number of matched patterns and subpatterns</font> <br />    <font color="Blue">for</font> (rc = <font color="Maroon">0</font>; rc &#60; nmatch; rc++) <br />        <font color="Blue">if</font> (pmatch[rc].rm_so == -<font color="Maroon">1</font>) <br />            <font color="Blue">break</font>; <br /> <br />    <font color="Blue">return</font> rc; <br />}</div><br /></div></li><li>Let's <span style="font-weight:bold">run</span> sample LoadRunner script and check the result:<br /><a href="http://bp0.blogger.com/_fjL89jPDk9E/SBmdc0d-TzI/AAAAAAAAAgc/rqgWoxQp1co/s1600-h/5_matchex_script_c.gif" onclick="pageTracker._trackPageview('/outgoing/bp0.blogger.com/_fjL89jPDk9E/SBmdc0d-TzI/AAAAAAAAAgc/rqgWoxQp1co/s1600-h/5_matchex_script_c.gif?referer=');"><img style="margin:0px auto 10px;display:block;text-align:center" src="http://bp0.blogger.com/_fjL89jPDk9E/SBmdc0d-TzI/AAAAAAAAAgc/rqgWoxQp1co/s400/5_matchex_script_c.gif" alt="" border="0"></a><span style="font-weight:bold">matchex()</span> function returns a number of matched patterns/subpatterns and fill an array in with information about each matched substring.<br /><br /><span style="font-weight:bold;color:rgb(0, 0, 153)"><br />What is an information about each matched substring?</span><br /><br />This info contains the <span style="font-weight:bold">offset</span> (rm_so) to the <span style="font-weight:bold">first character of each substring</span> and the <span style="font-weight:bold">offset</span> (rm_eo) to the <span style="font-weight:bold">first character after</span> the end of each substring, respectively.<br /><span style="font-weight:bold"><br /><span style="color:rgb(51, 51, 255)">Note1:</span> </span>The <span style="font-weight:bold">0th element</span> of the array relates to the <span style="font-weight:bold">entire portion</span> of string that was matched.<br /><span style="font-weight:bold;color:rgb(0, 0, 153)"><span style="color:rgb(51, 51, 255)">Note2:</span> </span><span style="font-weight:bold">Subsequent elements</span> of the array relate to the capturing <span style="font-weight:bold">subpatterns</span> of the regular expression.<br /><span style="font-weight:bold"><span style="color:rgb(51, 51, 255)">Note3:</span> Unused entries</span> in the array have both structure members set to <span style="font-weight:bold">-1</span>.<br /><br />Let's investigate it on out example. This is our subject string:<br /><a href="http://bp1.blogger.com/_fjL89jPDk9E/SBmljEd-T1I/AAAAAAAAAgs/StV5YVU432k/s1600-h/6_indexes.gif" onclick="pageTracker._trackPageview('/outgoing/bp1.blogger.com/_fjL89jPDk9E/SBmljEd-T1I/AAAAAAAAAgs/StV5YVU432k/s1600-h/6_indexes.gif?referer=');"><img style="margin:0px auto 10px;display:block;text-align:center" src="http://bp1.blogger.com/_fjL89jPDk9E/SBmljEd-T1I/AAAAAAAAAgs/StV5YVU432k/s400/6_indexes.gif" alt="" border="0"></a>The replay log shows offsets for matched substrings:<br /><ul><li>Action.c(<span style="color:rgb(51, 153, 153)">7</span>): Matched <span style="color:rgb(51, 153, 153)">3</span> patterns</li><li>Action.c(<span style="color:rgb(51, 153, 153)">10</span>): Start offset: <span style="color:rgb(51, 153, 153)">1</span>, End offset: <span style="color:rgb(51, 153, 153)">6</span></li><li>Action.c(<span style="color:rgb(51, 153, 153)">10</span>): Start offset: <span style="color:rgb(51, 153, 153)">2</span>, End offset: <span style="color:rgb(51, 153, 153)">5</span></li><li>Action.c(<span style="color:rgb(51, 153, 153)">10</span>): Start offset: <span style="color:rgb(51, 153, 153)">3</span>, End offset: <span style="color:rgb(51, 153, 153)">5</span></li></ul><br />Start offset: <span style="color:rgb(51, 153, 153)">1</span> and End offset: <span style="color:rgb(51, 153, 153)">6 </span>match substring <span style="font-weight:bold;color:rgb(0, 0, 153)">"bcdef"</span>.<br /><span style="font-weight:bold;color:rgb(51, 51, 255)">Note4:</span> End offset is the <span>first character </span><span style="font-weight:bold">after</span> the end the current substring. That's why character "g" (with index <span style="color:rgb(51, 153, 153)">6</span>) is not a part of matched string.<br /><br />As I've written in Note1, <span style="font-weight:bold;color:rgb(0, 0, 153)">"bcdef"</span><span style="color:rgb(0, 0, 0)"> is </span>the <span style="font-weight:bold">entire portion</span> of string that was matched.<br />Others items from an array relate to matched subpatterns.<br /><br /><br /><span style="font-weight:bold;color:rgb(0, 0, 153)">What is a subpattern in Regular Expression?<br /></span><br />It is a part of the RegExp pattern surrounded with parenthesis - "(" and ")".<br /><br />It's easy to get out the order of subpatterns. Just look through your pattern from left to right. When you find an open parenthes, this is a start of the current subpattern.<br />Subpattern can be embedded.<br /><br />So, others captured subpatterns are:<br /><ul><li>Start offset: <span style="color:rgb(51, 153, 153)">2</span>, End offset: <span style="color:rgb(51, 153, 153)">5</span> matches substring <span style="font-weight:bold;color:rgb(0, 0, 153)">"cde"</span>.<br />Note: current subpattern  is  <span style="font-weight:bold;color:rgb(0, 0, 153)">"([acqz](.*))"</span>. <span style="font-weight:bold"></span></li><li>Start offset: <span style="color:rgb(51, 153, 153)">3</span>, End offset: <span style="color:rgb(51, 153, 153)">5 </span>match substring <span style="font-weight:bold;color:rgb(0, 0, 153)">"de"</span>.<br /><span style="font-weight:bold;color:rgb(51, 51, 255)"></span>Note: current subpattern  is  <span style="font-weight:bold;color:rgb(0, 0, 153)">"(.*)"</span>.</li></ul><br />As you can see - this is not so difficult. :)<br />Regular Expressions can be very powerful and useful in LoadRunner.<br /><br /></li></ol><span style="font-weight:bold">Another example:</span><br /><br />Let's practise with an example I mentioned early:<br />For example, we have to <span style="font-weight:bold;color:rgb(0, 0, 153)">extract the name of month</span> from these strings:<br /><ul style="font-style:italic"><li>"abc 1st of May 2008xyz"</li><li>"abc 25th of February 2031"</li><li>etc<br /></li></ul>We can use the following pattern:<br /><ul><li>\d.+([A-Z]\w+)\s+\d{4}<br /></li></ul>The name of month will be matches by subpattern ([A-Z]\w+).<br /><br />Please, see LoadRunner script, which captures and prints name of months:<br /><a href="http://bp1.blogger.com/_fjL89jPDk9E/SBmvgEd-T2I/AAAAAAAAAg0/vApbtHMJPyY/s1600-h/7_example_c.gif" onclick="pageTracker._trackPageview('/outgoing/bp1.blogger.com/_fjL89jPDk9E/SBmvgEd-T2I/AAAAAAAAAg0/vApbtHMJPyY/s1600-h/7_example_c.gif?referer=');"><img style="margin:0px auto 10px;display:block;text-align:center" src="http://bp1.blogger.com/_fjL89jPDk9E/SBmvgEd-T2I/AAAAAAAAAg0/vApbtHMJPyY/s400/7_example_c.gif" alt="" border="0"></a>Note: Pay attention that I use arr[1] to get info about substring.<br />As you remember, arr[0] contains info about the entire matched pattern, arr[1], arr[2], and so on contain info about matched subpattern.<br /><br /><br /><span style="font-weight:bold">Summary:</span><br />I've explained, shown and demonstrated <span style="font-weight:bold">how to use Regular Expressions (RegExp) in LoadRunner</span>.<br />I hope, this knowledge will help you to create advanced LoadRunner scripts.<br /><br />--<br />Thank you, my readers!<br />Dmitry Motevich<img src="http://feeds.feedburner.com/~r/motevich/~4/281415093" height="1" width="1">]]></description>
		<wfw:commentRss>http://feeds.feedburner.com/~r/motevich/~3/281415093/examples-on-loadrunner-regular.html/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
<enclosure url="http://gnuwin32.sourceforge.net/downlinks/pcre-bin-zip.php" length="302480" type="application/zip" />
		</item>
	</channel>
</rss>
