Regex Pattern Matching in LoadRunner

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

# 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 …

7 comments to Regex Pattern Matching in LoadRunner

  • Sameh

    Thats awesome Koops…

  • 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

  • Tim

    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+)&";
    //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(&re, pattern, REG_EXTENDED))!= 0){
    regerror(status, &re, buf, 120);
    exit(2);
        }
     
    if(status = regexec( &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(&re);
    }

    Regards,
    Tim Koopmans

  • David Burke

    Tim,

    I found the pcre files at the following site:

    http://gnuwin32.sourceforge.net/packages/pcre.htm

  • Satish

    Hi, I want to encrypt the password while passing these parameterized values within the script. I am basically looking to find if we have a similar function in LoadRunner (as we have in QTP, SetSecure (“test123″))?
    Appreciate, if anyone can respond.

    Thanks!

  • Tim

    Hi Satish, check out the lr_decrypt function, this is probably what you are after …

    Cheers,
    Tim

  • [...] been identified how to enable Regular Expressions in LoadRunner. A big thanks to Charlie, Tim for getting this working, and Dmitry for proposing the [...]

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">