@Override public Object visit(StringBinaryComparison n, Void arg) { String first = (String) n.getLeftOperand().accept(this, null); String second = (String) n.getRightOperand().accept(this, null); Operator op = n.getOperator(); switch (op) { case EQUALSIGNORECASE: return first.equalsIgnoreCase(second) ? TRUE_VALUE : FALSE_VALUE; case EQUALS: return first.equals(second) ? TRUE_VALUE : FALSE_VALUE; case ENDSWITH: return first.endsWith(second) ? TRUE_VALUE : FALSE_VALUE; case CONTAINS: return first.contains(second) ? TRUE_VALUE : FALSE_VALUE; case PATTERNMATCHES: return second.matches(first) ? TRUE_VALUE : FALSE_VALUE; case APACHE_ORO_PATTERN_MATCHES: { Perl5Matcher matcher = new Perl5Matcher(); Perl5Compiler compiler = new Perl5Compiler(); Pattern pattern; try { pattern = compiler.compile(first); } catch (MalformedPatternException e) { throw new RuntimeException(e); } return matcher.matches(second, pattern) ? TRUE_VALUE : FALSE_VALUE; } default: log.warn("StringComparison: unimplemented operator!" + op); return null; } }
/** Convert specific starting hand to HoldemAtomicGroup object. @param groupSpec starting hand (e.g., AhKd, 8h3s) */ public HoldemAtomicGroup(String groupSpec) { myspec = groupSpec; myhands = new HashSet(); Perl5Compiler compiler = new Perl5Compiler(); Perl5Matcher matcher = new Perl5Matcher(); Pattern atomicPattern; try { atomicPattern = compiler.compile ("^([AKQJT98765432])([shdc])([AKQJT98765432])([shdc])$"); } catch (MalformedPatternException e) { throw new RuntimeException("BUG: " + e.toString()); } MatchResult result; if (matcher.matches(groupSpec, atomicPattern)) { result = matcher.getMatch(); int rank1 = Deck.parseRank(result.group(1)); int suit1 = Deck.parseSuit(result.group(2)); int rank2 = Deck.parseRank(result.group(3)); int suit2 = Deck.parseSuit(result.group(4)); addAtomic(rank1, suit1, rank2, suit2); } else { throw new IllegalArgumentException("unable to parse groupSpec: " + groupSpec); } }
/** 查找 */ 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()); } }
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); }
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; }
/** 分组 */ 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; }
/** * 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 String processReplacementOperations(String expression, String[] tableNames) { String returnedExpression = expression; for (int i = 0; i < tableNames.length; i++) { String connectionName = tableNames[i]; recompilePatternIfNecessary( StringHelper.replacePrms( "\\$[\\s\\r\\n]*({0})[\\s\\r\\n]*\\[", //$NON-NLS-1$ new Object[] {connectionName})); if (returnedExpression != null) { matcher.setMultiline(true); Perl5Substitution substitution = new Perl5Substitution( "\\$" + "$1->" //$NON-NLS-1$ //$NON-NLS-2$ + "[", Perl5Substitution.INTERPOLATE_ALL); // $NON-NLS-1$ returnedExpression = Util.substitute( matcher, pattern, substitution, returnedExpression, Util.SUBSTITUTE_ALL); } recompilePatternIfNecessary( StringHelper.replacePrms( "@[\\s\\r\\n]*({0})\\b", new Object[] {connectionName})); // $NON-NLS-1$ if (returnedExpression != null) { matcher.setMultiline(true); Perl5Substitution substitution = new Perl5Substitution( "@\\$" + "$1" // $NON-NLS-1$ //$NON-NLS-2$ , Perl5Substitution.INTERPOLATE_ALL); returnedExpression = Util.substitute( matcher, pattern, substitution, returnedExpression, Util.SUBSTITUTE_ALL); } } return returnedExpression; }
/** * 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; }
private boolean matchExpression(String regex, String expression) { PatternCompiler compiler = new Perl5Compiler(); try { Pattern pattern = compiler.compile( "\\b(" + UpdateContextVariablesHelper.replaceSpecialChar(regex) + ")(\\b|\\_)"); //$NON-NLS-1$ //$NON-NLS-2$ PatternMatcher matcher = new Perl5Matcher(); ((Perl5Matcher) matcher).setMultiline(true); if (matcher.contains(expression, pattern)) { return true; } } catch (MalformedPatternException e) { // } return false; }
/** {@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); } }
public static boolean isMatches(String strQuery, String strPattern) { return matcher.matches(strQuery, PatternUtils.getPattern(strPattern)); }