Previously I identified how to setup pattern matching with Regex in LoadRunner.
Here’s how you implement search and replace functionality with the same POSIX libraries used in the previous example.
Create a replace function in your favourite headers file or wherever you keep your framework’y stuff… You also need the pcre3.dll and pcreposix.h header added as files to your script.
replace(const char *string, char *pattern, char *replace, char *match) { int length; int status; int eflag; char buf[1024] = ""; char out[1024] = ""; regex_t re; regmatch_t pmatch[128]; lr_load_dll("pcre3.dll"); if((status = regcomp(&re, pattern, REG_EXTENDED)) != 0){ regerror(status, &re, buf, 120); lr_output_message("Match PCRE Exit 2"); return 2; } while(status = regexec( &re, string, 1, pmatch, eflag)== 0){ strncat(out, string, pmatch[0].rm_so); strcat(out, replace); string += pmatch[0].rm_eo; eflag = REG_NOTBOL; } strcat(out, string); lr_save_string(out, match); }
Then anywhere in your actions you can call on this function, passing it the string buffer you wish to operate on, a search value, a replace value and the name of the LoadRunner parameter you want the result saved into.
lr_save_string("FOO%20BAR%20DING", "buffer"); replace(lr_eval_string("{buffer}"),"%20"," ", "custom");
What you’ll end up with is something like this:
Home.c(6): Notify: Saving Parameter "buffer = FOO%20BAR%20DING"
Home.c(7): Notify: Parameter Substitution: parameter "buffer" = "FOO%20BAR%20DING"
framework.h(135): Notify: Saving Parameter "custom = FOO BAR DING"
Enjoy.
