jmeter-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Yanroy <>
Subject Re: using regex with beanshell
Date Tue, 01 Aug 2006 20:22:34 GMT

Actually, yes, just moments ago I finally came up with a solution.  The code
isn't very pretty (I suspect beanshell never is), but it does what I need it
to do.  Hopefully it'll help you too.  I use the java regex library to parse
the result of a jmeter sampler.  This code is what I typed into a beanshell
assertion element in jmeter:

import java.util.regex.*;

String pStr = ".*?Assistment: \\((\\d+?)\\).*";
print("Regex Pattern: " + pStr);
Pattern idPattern = Pattern.compile(pStr, Pattern.UNIX_LINES |
Matcher idMatcher =
boolean matched = idMatcher.matches();

if (matched) {
print("Matched ID: " +;
else {
print("didn't match id");
Failure = true;
FailureMessage = "Couldn't extract ID from question";

[end code]

This matches the string "Assistment: (2100)" and extracts 2100 from it (or
any other number).  That string is embedded in the HTML of a page.  I
specify the UNIX_LINES flag to the regex because I'm using linux... idk if
it's really necessary.  Also, you'll note the plethora of backslashes...
this is because the java/beanshell compiler escapes the backslash characters
once, then the regex engine does it again.  Of course, this code doesn't do
anything useful other than fail the assertion if it couldn't find the
number.  Have fun with it.

View this message in context:
Sent from the JMeter - User forum at

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message