Regex Pattern Matching in LoadRunner

17 April, 2008 – 10:47 am

I quite literally stole this idea from Charlie at PerformanceEngineering in which he posted a response to a challenge by Dmitry on how to get regular expressions up and running within LoadRunner.

I’m a huge fan of regex, and thanks to these guys, now have a way of implementing at least pattern matching with LoadRunner. Ideally I’d like to be able to include pattern matching & replacing, like Ruby or Perl does in one line but for now am happy for pattern matching.

Read on for the changes I made to Charlies code to abstract matching functionality and call it as a function within other LoadRunner actions.

First thing, follow these instructions posted by Charlie

# First, download the Binaries and Developer Files for PCRE (Perl Compatible Regular Expressions):
http://gnuwin32.sourceforge.net/pakages/pcre.htm

# Unzip (both archives) into c:\pcre

# Modify c:\pcre\include\pcre.h by commenting out the include for stdlib.h:

//#include <stdlib.h>

# In your LoadRunner script, add to globals.h:

#include "c:\\pcre\\include\\pcre.h"

Then from here on I differ slightly by adding a couple of functions to my vuser_init as per the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
vuser_init()
{
    return 0;
}
 
buffer()
{
	// This will save a 2MB buffer of the response body when called.
	web_set_max_html_param_len("2097152"); //2MB buffer
	web_reg_save_param("buffer", "LB=", "RB=", "Search=Body", LAST);
	return 0;
 
}
 
match(const char *string, char *pattern)
{
	// This will return 1 for a match, or 0 for a non-match / error
	int status;
	pcre *re;
	lr_load_dll("c:\\pcre\\bin\\pcre3.dll");
 
	if (regcomp(&re, pattern) != 0) {
		return(0); /* Report error. */
	}
	status = regexec(&re, string, (size_t) 0, NULL, 0);
	regfree(&re);
	if (status != 0) {
		return(0); /* Report error. */
	}
	return(1);
}

Then from your main action you can call that match function in a Perl’ish kind of way as per:

1
2
rc = match(lr_eval_string("{buffer}"),"[\\w-]+");
lr_message("Return Code = %d",rc);

Make sure you create an int to store your return code.
Attached are example vuser_init and actions for you.
regex vuser_init :: regex action
:)

Thanks Charlie and Dimitry! Would like to see this challenge extended to search & replace functionality using regex …

Share it: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Netscape
  • Reddit
  • Slashdot
  • Technorati
  • YahooMyWeb
  1. 3 Responses to “Regex Pattern Matching in LoadRunner”

  2. Thats awesome Koops…

    By Sameh on Apr 17, 2008

  3. Hello, Tim.

    I appreciated your efforts on working with Regular Expressions in LoadRunner.

    I moved forward Charlie’s and your ideas on LoadRunner RegExps. So, now we can use LR RegExp to match subpatterns and to capture (=to extract) all matched substrings.

    The solution is located here:
    Examples on LoadRunner Regular Expressions

    By Dmitry Motevich on May 2, 2008

  4. For my own reference, you can also get regex pattern matching with this approach. You need to comment out //#include in the pcreposix.h header for this to work:

    request_num() {
    	int  status;
        int  eflag;
        char buf[256];
    	char out[128];
    	char *pattern = "requestnum=(\\d+)'|requestnum=(\\d+)&amp;";
    	//char *pattern = "sen(\\w+)|s\\wn(\\w+)";
    	char *string  = lr_eval_string("{request_num_buff}");
        regex_t re;
        regmatch_t pmatch[128];
    	lr_load_dll("c:\\pcre\\bin\\pcre3.dll");
     
    	if((status = regcomp(&amp;re, pattern, REG_EXTENDED))!= 0){
    		regerror(status, &amp;re, buf, 120);
    		exit(2);
        }
     
    	if(status = regexec( &amp;re, string, 10, pmatch, eflag)== 0){
    		strncpy(out, string + pmatch[1].rm_so, pmatch[1].rm_eo - pmatch[1].rm_so);
            lr_save_string(out, "requestnum"); 
    		lr_output_message("Notify: Parameter requestnum was found: %s\n",out);
    		eflag = REG_NOTBOL;
        }
        regfree(&amp;re);
    }

    Regards,
    Tim Koopmans

    By Tim on Jul 14, 2008

Post a Comment

*
To prove that you're not a bot, enter this code
Anti-Spam Image