4

Is there any way to set up a regex extractor with a regex that consists of dynamic variable (e.g, ${var}).

The rationale asking is because one section of my test plan is to get the User ID of a certain user account from the html response, so subsequently Jmeter will continue doing its business with that User ID as the reference. If I only worry about 1 thread to the test plan, it will be as simple as below

<.*id=(/d+).*value="johndoe" 

But I want the test plan to be flexible enough to handle multiple thread with each thread represents a unique user, so the regular expression will have to be something like below

<.*id=(/d+).*value="${USERNAME}"

One or two of advices on how to achieve this will be appreciated. If it's not achievable, an alternative way will also be good

Thanks

Daniel
  • 631
  • 10
  • 18

8 Answers8

4

Greetings,

As a soon-to-be Jmeter lover, you'll find this happens often. You simply need to escape the special characters in your regex. The dollar sign has special meaning in PERL regular expressions, so we need to tell the regex to use a literal $:

<.*id=(/d+).*value="\${USERNAME}"

Also, the id section is a bit greedy. I would recommend:

<.*id=(/d+?) value="${USERNAME}"
BlackGaff
  • 7,579
  • 1
  • 38
  • 45
  • Hi, I have done some testing but still can not get it working on my machine. Now I only have variable written in the regular expression extractor as such: \$\{APP\_NAME\}. The variable is expected to take the actual regular expression as defined user defined variable called APP_NAME. This doesn't create the effect I would like to have. Some opinion will be appreicated – Daniel May 10 '11 at 03:31
  • You don't need to escape the curly brackets. Have you tried just \${APP_NAME} ? – BlackGaff May 10 '11 at 19:50
  • 1
    I'm using JMeter 2.13 and I could use a variable without escaping it. See @nikolay-miroshnichenko's answer. – Ricardo van den Broek Oct 16 '15 at 23:10
3

I had a similar problem and simply escaping the $ character didn't work for me.

Instead I had to use the __V() function.
So in this example, the regular expression would be like below:

<.*id=(/d+).*value="${__V(${USERNAME})}"

I didn't have to escape the $ character. I'm using JMeter 2.9

spark
  • 31
  • 1
2

In Apache JMeter 2.9, the correct expression the include a variable value ${MYVARIABLE} inside a Regular Expression is:

__V(${MYVARIABLE})

This should be placed in the desired place in the regex.

Works fine!!!

Carlos AG
  • 1,078
  • 1
  • 12
  • 16
1

For me there is no need to escape anything (in Jmeter variables syntax). I'm using JMeter 2.9

Setting only variable (containing the whole regex, maybe dynamically generated) like ${regex} in the field "Regular Expression" also works.

  • I'm using JMeter 2.13 and I can confirm this: no escaping was needed to use a variable. If that doesn't seem to be working, then maybe PEBKAC... that turned out to be the reason for me, anyway. :-) – Ricardo van den Broek Oct 16 '15 at 23:08
0

This issue was irritating me too. The only solution that worked for me was adding a BeanShell Sampler with code of this sort:

String S = "blah-blah-blah...\\\d...${My variable}...blah-blah-blah";

vars.put("RegExp", S);

And Then in Regular Expression Extractor a used only this:

${RegExp}

NB Note that for some reason \d was needed to be screened with THREE backslashes...facepalm.jpg

Dev2rights
  • 3,469
  • 3
  • 25
  • 42
0

For Jmeter 3.1 I had to use the ${__V(${MYVAR})} syntax as @spark and @carlos AG stated

0

The only way I did this was, First creating a String, concatenating the variables and the rest of the pattern and saving this into a variable (this in a PostProcessor). And putting it in the regular expression extractor.

vcv
  • 1
  • 1
0

I described very similar problem on jMeter 5.5-Snapshot 3a74a92 with working solution here: jMeter Regular Expression Extractor with use of variable in regexp

user1337
  • 175
  • 7