jmeter-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Yanroy <rmea...@wpi.edu>
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 |
Pattern.DOTALL);
Matcher idMatcher =
idPattern.matcher(SampleResult.getResponseDataAsString());
boolean matched = idMatcher.matches();

if (matched) {
print("Matched ID: " + idMatcher.group(1));
}
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.

Ryan
-- 
View this message in context: http://www.nabble.com/using-regex-with-beanshell-tf2029950.html#a5602388
Sent from the JMeter - User forum at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: jmeter-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jmeter-user-help@jakarta.apache.org


Mime
View raw message