private String getDomain(String url) { PatternMatcher matcher = new Perl5Matcher(); PatternCompiler compiler = new Perl5Compiler(); PatternMatcherInput input = new PatternMatcherInput(url); Pattern pattern = null; String patternString = HTTP_PATTERN; try { pattern = compiler.compile(patternString); } catch (MalformedPatternException e) { logger.error("Incorrect pattern: " + patternString, e); } while (matcher.contains(input, pattern)) { MatchResult result = matcher.getMatch(); String sResult = result.group(1); logger.info("Match: " + sResult); return sResult; } patternString = WWW_PATTERN; try { pattern = compiler.compile(patternString); } catch (MalformedPatternException e) { logger.error("Incorrect pattern: " + patternString, e); } while (matcher.contains(input, pattern)) { MatchResult result = matcher.getMatch(); String sResult = result.group(1); logger.info("Match: " + sResult); return sResult; } 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 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); }
/** * Creates the variables:<br> * basename_gn, where n=0...# of groups<br> * basename_g = number of groups (apart from g0) */ private void saveGroups(JMeterVariables vars, String basename, MatchResult match) { StringBuilder buf = new StringBuilder(); buf.append(basename); buf.append("_g"); // $NON-NLS-1$ int pfxlen = buf.length(); String prevString = vars.get(buf.toString()); int previous = 0; if (prevString != null) { try { previous = Integer.parseInt(prevString); } catch (NumberFormatException e) { log.warn("Could not parse " + prevString + " " + e); } } // Note: match.groups() includes group 0 final int groups = match.groups(); for (int x = 0; x < groups; x++) { buf.append(x); vars.put(buf.toString(), match.group(x)); buf.setLength(pfxlen); } vars.put(buf.toString(), Integer.toString(groups - 1)); for (int i = groups; i <= previous; i++) { buf.append(i); vars.remove(buf.toString()); // remove the remaining _gn vars buf.setLength(pfxlen); } }
public Value match(Value str) { PatternMatcher matcher = getMatcher(); // Do the matching PatternMatcherInput jStr = new PatternMatcherInput(string(str)); Pair result = null; Pair prev = null; boolean found = false; while (matcher.contains(jStr, pattern)) { found = true; MatchResult matchResult = matcher.getMatch(); for (int i = 0, length = matchResult.groups(); i < length; i++) { Pair m = new Pair(new SchemeString(matchResult.group(i)), EMPTYLIST); if (result == null) result = prev = m; else { prev.setCdr(m); prev = m; } } } if (!found) return FALSE; else return result; }
private void saveGroups(MatchResult result) { if (result != null) { JMeterVariables vars = getVariables(); for (int x = 0; x < result.groups(); x++) { vars.put(name + "_g" + x, result.group(x)); } } }
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]); }
public RubyValue captures() { RubyArray ar = new RubyArray(); for (int i = 1; i < result_.groups(); i++) { String res = result_.group(i); if (res == null) ar.add(RubyConstant.QNIL); else ar.add(ObjectFactory.createString(res)); } return ar; }
public RubyValue to_a() { RubyArray ar = new RubyArray(); for (int i = 0; i < result_.groups(); i++) { String str = result_.group(i); if (str != null) ar.add(ObjectFactory.createString(str)); else ar.add(RubyConstant.QNIL); } return ar; }
public void appendSubstitution( StringBuffer stringBuffer, MatchResult matchResult, int i, PatternMatcherInput patternMatcherInput, PatternMatcher patternMatcher, Pattern pattern) { String hex = matchResult.group(1); char c = (char) Integer.parseInt(hex, 16); stringBuffer.append(c); }
/** * insert正则表达式解析insert语句 如果insert语句与正则表达式匹配,本方法根据表达式对语句的分组 * 将insert语句拆分为6部分,并且将这几部分存放到字符串数组返回 分别为: 1.insert关键字 2.into关键字 3.表名称 4.表字段组 * 5.values关键字 6.插入字段值组 * * 比如insert语句: Insert into oa_meetingpromptsound ( soundCode , soundName , * soundFileName ) values ( '。.尹标平','bb','d()d' ) 将被分解为以下部分: 1.Insert 2.into * 3.oa_meetingpromptsound 4.( soundCode , soundName , soundFileName ) * 5.values 6.( '。.尹标平','bb','d()d' ) */ public static String[] parserInsert(String insert) { /** * 定义insert语句的正则表达式 该表达式将insert语句拆分为6部分,分别为: 1.insert关键字 2.into关键字 3.表名称 * 4.表字段组 5.values关键字 6.插入字段值组 比如insert语句: Insert into * oa_meetingpromptsound ( soundCode , soundName , soundFileName ) * values ( '。.尹标平','bb','d()d' ) 将被分解为以下部分: 1.Insert 2.into * 3.oa_meetingpromptsound 4.( soundCode , soundName , soundFileName ) * 5.values 6.( '。.尹标平','bb','d()d' ) */ String patternStr = "\\s*(insert)\\s+" + // 解析insert关键词 "(into)\\s+" + // 解析into关键词 "([^\\(^\\s]+)\\s*" + // 解析表名称 "(\\([^\\)]+\\))\\s*" + // 解析表字段 "(values)\\s*" + // 解析value关键词 "(\\(.*(.*\n*)*.*)"; // 解析字段值 /** * 编译正则表达式patternStr,并用该表达式与传入的sql语句进行模式匹配, * 如果匹配正确,则从匹配对象中提取出以上定义好的6部分,存放到数组中并返回 该数组 */ PatternCompiler compiler = new Perl5Compiler(); Pattern pattern = null; try { pattern = compiler.compile(patternStr, Perl5Compiler.CASE_INSENSITIVE_MASK); } catch (MalformedPatternException e) { e.printStackTrace(); return null; } PatternMatcher matcher = new Perl5Matcher(); MatchResult result = null; String[] tokens = null; boolean match = matcher.matches(insert, pattern); if (match) { result = matcher.getMatch(); tokens = new String[6]; for (int i = 0; i < 6; i++) { tokens[i] = result.group(i + 1).trim(); } } return tokens; }
/** 分组 */ 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)); } } }
/** 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 String generateResult(MatchResult match) { saveGroups(match); StringBuffer result = new StringBuffer(); for (int a = 0; a < template.length; a++) { if (template[a] instanceof String) { result.append(template[a]); } else { result.append(match.group(((Integer) template[a]).intValue())); } } JMeterVariables vars = getVariables(); vars.put(name, result.toString()); return result.toString(); }
// @RubyLevelMethod(name="[]") public RubyValue aref(RubyValue arg) { if (arg instanceof RubyFixnum) { int index = arg.toInt(); if (index < 0) index += result_.groups(); String res = result_.group(index); if (res == null) return RubyConstant.QNIL; return ObjectFactory.createString(res); } else if (arg instanceof RubyString) { // TODO: implement m = /(?<foo>a+)b/.match("ccaaab"); m["foo"] #=> "aaa" throw new RuntimeException("Not implemented"); } RubyArray ar = (RubyArray) to_a(); return ar.aref(arg); }
private String generateResult(MatchResult match) { StringBuilder result = new StringBuilder(); for (Object obj : template) { if (log.isDebugEnabled()) { log.debug( "RegexExtractor: Template piece " + obj + " (" + obj.getClass().getSimpleName() + ")"); } if (obj instanceof Integer) { result.append(match.group(((Integer) obj).intValue())); } else { result.append(obj); } } if (log.isDebugEnabled()) { log.debug("Regex Extractor result = " + result.toString()); } return result.toString(); }
public static String[] parserValues(String values) { String patternStr = "([^\\,]*)[\\,]?"; // 解析字段值 String patternStr1 = "('?[^\\,]*'?)[\\,]?"; // 解析字段值 /** * 编译正则表达式patternStr,并用该表达式与传入的sql语句进行模式匹配, * 如果匹配正确,则从匹配对象中提取出以上定义好的6部分,存放到数组中并返回 该数组 */ String[] ret = RegexUtil.containWithPatternMatcherInput(values,patternStr1); PatternCompiler compiler = new Perl5Compiler(); Pattern pattern = null; try { pattern = compiler.compile(patternStr, Perl5Compiler.CASE_INSENSITIVE_MASK); } catch (MalformedPatternException e) { e.printStackTrace(); return null; } PatternMatcher matcher = new Perl5Matcher(); MatchResult result = null; String[] tokens = null; boolean match = matcher.matches(values.trim(), pattern); System.out.println(match); if (match) { result = matcher.getMatch(); tokens = new String[6]; for (int i = 0; i < 6; i++) { tokens[i] = result.group(i + 1).trim(); System.out.println(tokens[i]); } } return tokens; }
private void initTemplate() { if (template != null) { return; } // Contains Strings and Integers List<Object> combined = new ArrayList<Object>(); String rawTemplate = getTemplate(); PatternMatcher matcher = JMeterUtils.getMatcher(); Pattern templatePattern = JMeterUtils.getPatternCache() .getPattern( "\\$(\\d+)\\$" // $NON-NLS-1$ , Perl5Compiler.READ_ONLY_MASK & Perl5Compiler.SINGLELINE_MASK); if (log.isDebugEnabled()) { log.debug("Pattern = " + templatePattern.getPattern()); log.debug("template = " + rawTemplate); } int beginOffset = 0; MatchResult currentResult; PatternMatcherInput pinput = new PatternMatcherInput(rawTemplate); while (matcher.contains(pinput, templatePattern)) { currentResult = matcher.getMatch(); final int beginMatch = currentResult.beginOffset(0); if (beginMatch > beginOffset) { // string is not empty combined.add(rawTemplate.substring(beginOffset, beginMatch)); } combined.add(Integer.valueOf(currentResult.group(1))); // add match as Integer beginOffset = currentResult.endOffset(0); } if (beginOffset < rawTemplate.length()) { // trailing string is not empty combined.add(rawTemplate.substring(beginOffset, rawTemplate.length())); } if (log.isDebugEnabled()) { log.debug("Template item count: " + combined.size()); for (Object o : combined) { log.debug(o.getClass().getSimpleName() + " '" + o.toString() + "'"); } } template = combined; }
private void processComponentStart( String tagName, String jwcId, boolean emptyTag, int startLine, int cursorStart, ILocation startLocation) throws TemplateParseException { if (jwcId.equalsIgnoreCase(CONTENT_ID)) { processContentTag(tagName, startLine, cursorStart, emptyTag); return; } boolean isRemoveId = jwcId.equalsIgnoreCase(REMOVE_ID); if (_ignoring && !isRemoveId) templateParseProblem( Tapestry.format( "TemplateParser.component-may-not-be-ignored", tagName, Integer.toString(startLine)), startLocation, startLine, cursorStart); String type = null; boolean allowBody = false; if (_patternMatcher.matches(jwcId, _implicitIdPattern)) { MatchResult match = _patternMatcher.getMatch(); jwcId = match.group(IMPLICIT_ID_PATTERN_ID_GROUP); type = match.group(IMPLICIT_ID_PATTERN_TYPE_GROUP); String libraryId = match.group(IMPLICIT_ID_PATTERN_LIBRARY_ID_GROUP); String simpleType = match.group(IMPLICIT_ID_PATTERN_SIMPLE_TYPE_GROUP); // If (and this is typical) no actual component id was specified, // then generate one on the fly. // The allocated id for anonymous components is // based on the simple (unprefixed) type, but starts // with a leading dollar sign to ensure no conflicts // with user defined component ids (which don't allow dollar signs // in the id). if (jwcId == null) jwcId = _idAllocator.allocateId("$" + simpleType); try { allowBody = _delegate.getAllowBody(libraryId, simpleType, startLocation); } catch (ApplicationRuntimeException e) { // give subclasses a chance to handle and rethrow templateParseProblem(e, startLine, cursorStart); } } else { if (!isRemoveId) { if (!_patternMatcher.matches(jwcId, _simpleIdPattern)) templateParseProblem( Tapestry.format( "TemplateParser.component-id-invalid", tagName, Integer.toString(startLine), jwcId), startLocation, startLine, cursorStart); if (!_delegate.getKnownComponent(jwcId)) templateParseProblem( Tapestry.format( "TemplateParser.unknown-component-id", tagName, Integer.toString(startLine), jwcId), startLocation, startLine, cursorStart); try { allowBody = _delegate.getAllowBody(jwcId, startLocation); } catch (ApplicationRuntimeException e) { // give subclasses a chance to handle and rethrow templateParseProblem(e, startLine, cursorStart); } } } // Ignore the body if we're removing the entire tag, // of if the corresponding component doesn't allow // a body. boolean ignoreBody = !emptyTag && (isRemoveId || !allowBody); if (_ignoring && ignoreBody) templateParseProblem( Tapestry.format("TemplateParser.nested-ignore", tagName, Integer.toString(startLine)), new Location(_resourceLocation, startLine), startLine, cursorStart); if (!emptyTag) pushNewTag(tagName, startLine, isRemoveId, ignoreBody); // End any open block. addTextToken(cursorStart - 1); if (!isRemoveId) { addOpenToken(tagName, jwcId, type, startLocation); if (emptyTag) _tokens.add(_factory.createCloseToken(tagName, getCurrentLocation())); } advance(); }
/** {@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 String toString() { return result_.group(0); }