public void process() { Sampler sampler = JMeterContextService.getContext().getCurrentSampler(); SampleResult responseText = JMeterContextService.getContext().getPreviousResult(); if (responseText == null) { return; } initRegex(getArgumentName()); String text = new String(responseText.getResponseData()); Perl5Matcher matcher = JMeterUtils.getMatcher(); String value = ""; if (matcher.contains(text, case1)) { MatchResult result = matcher.getMatch(); value = result.group(1); } else if (matcher.contains(text, case2)) { MatchResult result = matcher.getMatch(); value = result.group(1); } else if (matcher.contains(text, case3)) { MatchResult result = matcher.getMatch(); value = result.group(1); } else if (matcher.contains(text, case4)) { MatchResult result = matcher.getMatch(); value = result.group(1); } modify((HTTPSampler) sampler, value); }
/** 查找 */ public static void simpleContains() throws Exception { Pattern pattern = new Perl5Compiler().compile("\\d+"); Perl5Matcher matcher = new Perl5Matcher(); PatternMatcherInput matcherInput = new PatternMatcherInput("北京2008年8月08日20时"); System.out.println("simpleContains:"); while (matcher.contains(matcherInput, pattern)) { MatchResult result = matcher.getMatch(); System.out.println(result.toString()); } }
boolean hasRequiredCharacterMix(String str) { // Must have at least 3 of: upper alpha, lower alpha, numeric, special int cnt = 0; Perl5Matcher matcher = RegexpUtil.getMatcher(); for (Pattern pat : charPats) { if (matcher.contains(str, pat)) { cnt++; } } return cnt >= 3; }
/** * Return true if the regexp pattern is found to occur in the input string. * * @param input the input string * @param p the pattern */ public static boolean regexp(String input, String p) { try { Pattern pattern = compiler.compile(p); return sMatcher.contains(input, pattern); } catch (MalformedPatternException e) { // We should probably print something to a debug log or something to mention that the pattern // we tested // threw an error and probably has some bogus regexp syntax. return false; } }
/** 分组 */ public static void simpleResults() throws Exception { Pattern pattern = new Perl5Compiler().compile("(\\d+\\.\\d+\\.\\d+\\.\\d+)@(\\d{2}/\\d{2}/\\d{4})"); Perl5Matcher matcher = new Perl5Matcher(); PatternMatcherInput matcherInput = new PatternMatcherInput("202.108.9.38@08/10/2008"); System.out.println("simpleResults:"); while (matcher.contains(matcherInput, pattern)) { MatchResult result = matcher.getMatch(); for (int i = 0; i < result.groups(); i++) { System.out.printf("%s : %s\n", i, result.group(i)); } } }
/** * Return the value associated with the first pattern that the string matches, or the specified * default value if none, considering only patterns whose associated value is less than or equal * to maxPri. */ public int getMatch(String str, int dfault, int maxPri) { Perl5Matcher matcher = RegexpUtil.getMatcher(); for (Map.Entry<Pattern, Integer> ent : patternMap.entrySet()) { if (ent.getValue() <= maxPri) { Pattern pat = ent.getKey(); if (matcher.contains(str, pat)) { log.debug2("getMatch(" + str + "): " + ent.getValue()); return ent.getValue(); } } } log.debug2("getMatch(" + str + "): default: " + dfault); return dfault; }
public TableEntryLocation[] parseTableEntryLocations(String expression) { resultList.clear(); if (expression != null) { matcher.setMultiline(true); if (patternMatcherInput == null) { patternMatcherInput = new PatternMatcherInput(expression); } else { patternMatcherInput.setInput(expression); } recompilePatternIfNecessary(locationPattern); while (matcher.contains(patternMatcherInput, pattern)) { MatchResult matchResult = matcher.getMatch(); resultList.add(new TableEntryLocation(matchResult.group(1), matchResult.group(2))); } } return resultList.toArray(new TableEntryLocation[0]); }
private int matchStrings( int matchNumber, Perl5Matcher matcher, Pattern pattern, List<MatchResult> matches, int found, String inputString) { PatternMatcherInput input = new PatternMatcherInput(inputString); while (matchNumber <= 0 || found != matchNumber) { if (matcher.contains(input, pattern)) { log.debug("RegexExtractor: Match found!"); matches.add(matcher.getMatch()); found++; } else { break; } } return found; }
/** * Make sure the response satisfies the specified assertion requirements. * * @param response an instance of SampleResult * @return an instance of AssertionResult */ private AssertionResult evaluateResponse(SampleResult response) { boolean pass = true; boolean not = (NOT & getTestType()) > 0; AssertionResult result = new AssertionResult(); if (response.getResponseData() == null) { return setResultForNull(result); } String responseString = new String(response.getResponseData()); try { // Get the Matcher for this thread Perl5Matcher localMatcher = (Perl5Matcher) matcher.get(); PropertyIterator iter = getTestStrings().iterator(); while (iter.hasNext()) { String stringPattern = iter.next().getStringValue(); Pattern pattern = patternCache.getPattern(stringPattern, Perl5Compiler.READ_ONLY_MASK); boolean found; if ((CONTAINS & getTestType()) > 0) { found = localMatcher.contains(responseString, pattern); } else { found = localMatcher.matches(responseString, pattern); } pass = not ? !found : found; if (!pass) { result.setFailure(true); result.setFailureMessage( "Test Failed, expected " + notMessage + failMessage + stringPattern); break; } } if (pass) { result.setFailure(false); } result.setError(false); } catch (MalformedCachePatternException e) { result.setError(true); result.setFailure(false); result.setFailureMessage("Bad test configuration" + e); } return result; }
/** Parse a string of the form [x1,y1},[x2,y2},...,[xN,yN} into a list of Points ([int,int]) */ public static List<Point> parseString(String str) throws NumberFormatException { if (StringUtil.isNullString(str)) { throw new IllegalArgumentException("Must supply non-empty string"); } ArrayList<Point> res = new ArrayList<Point>(); Perl5Matcher matcher = RegexpUtil.getMatcher(); while (matcher.contains(str, ONE_POINT_PAT)) { MatchResult matchResult = matcher.getMatch(); String xstr = matchResult.group(1); String ystr = matchResult.group(2); str = matchResult.group(3); try { int x = Integer.parseInt(xstr); int y = Integer.parseInt(ystr); res.add(new Point(x, y)); } catch (NumberFormatException e) { throw new IllegalArgumentException("bad point [" + xstr + "," + ystr + "] in " + str); } } res.trimToSize(); return res; }
/** {@inheritDoc} */ @Override public Iterator<URL> getEmbeddedResourceURLs( String userAgent, byte[] html, URL baseUrl, URLCollection urls, String encoding) throws HTMLParseException { Pattern pattern = null; Perl5Matcher matcher = null; try { matcher = JMeterUtils.getMatcher(); PatternMatcherInput input = localInput.get(); // TODO: find a way to avoid the cost of creating a String here -- // probably a new PatternMatcherInput working on a byte[] would do // better. input.setInput(new String(html, encoding)); pattern = JMeterUtils.getPatternCache() .getPattern( REGEXP, Perl5Compiler.CASE_INSENSITIVE_MASK | Perl5Compiler.SINGLELINE_MASK | Perl5Compiler.READ_ONLY_MASK); while (matcher.contains(input, pattern)) { MatchResult match = matcher.getMatch(); String s; if (log.isDebugEnabled()) { log.debug("match groups " + match.groups() + " " + match.toString()); } // Check for a BASE HREF: for (int g = 1; g <= NUM_BASE_GROUPS && g <= match.groups(); g++) { s = match.group(g); if (s != null) { if (log.isDebugEnabled()) { log.debug("new baseUrl: " + s + " - " + baseUrl.toString()); } try { baseUrl = ConversionUtils.makeRelativeURL(baseUrl, s); } catch (MalformedURLException e) { // Doesn't even look like a URL? // Maybe it isn't: Ignore the exception. if (log.isDebugEnabled()) { log.debug("Can't build base URL from RL " + s + " in page " + baseUrl, e); } } } } for (int g = NUM_BASE_GROUPS + 1; g <= match.groups(); g++) { s = match.group(g); if (s != null) { if (log.isDebugEnabled()) { log.debug("group " + g + " - " + match.group(g)); } urls.addURL(s, baseUrl); } } } return urls.iterator(); } catch (UnsupportedEncodingException e) { throw new HTMLParseException(e.getMessage(), e); } catch (MalformedCachePatternException e) { throw new HTMLParseException(e.getMessage(), e); } finally { JMeterUtils.clearMatcherMemory(matcher, pattern); } }