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; }
public WildcardFilenameFilter(String regex) { try { matcher_ = new Perl5Matcher(); PatternCompiler compiler = new GlobCompiler(); pattern_ = compiler.compile(regex); } catch (MalformedPatternException e) { throw (new IllegalArgumentException(e.getMessage())); } }
/** * 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; }
@Override public boolean eval(Map<String, Object> context) { Object fieldVal = this.fieldAcsr.get(context); String expr = this.exprExdr.expandString(context); Pattern pattern; try { pattern = compiler.compile(expr); } catch (MalformedPatternException e) { String errMsg = "Error in evaluation in if-regexp in screen: " + e.toString(); Debug.logError(e, errMsg, module); throw new IllegalArgumentException(errMsg); } String fieldString = null; try { fieldString = (String) ObjectType.simpleTypeConvert( fieldVal, "String", null, (TimeZone) context.get("timeZone"), (Locale) context.get("locale"), true); } catch (GeneralException e) { Debug.logError(e, "Could not convert object to String, using empty String", module); } // always use an empty string by default if (fieldString == null) fieldString = ""; return matcher.matches(fieldString, pattern); }
public RegexFunction() { valueIndex = between = name = ""; try { templatePattern = compiler.compile("\\$(\\d+)\\$"); } catch (MalformedPatternException e) { log.error("", e); } }
private boolean isFirstElementGroup(String rawData) { try { Pattern pattern = compiler.compile("^\\$\\d+\\$"); return new Perl5Matcher().contains(rawData, pattern); } catch (MalformedPatternException e) { log.error("", e); return false; } }
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; }
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; }
public static Pattern getPattern(String strPattern) { if (patterns.containsKey(strPattern) && patterns.get(strPattern).get() != null) { return patterns.get(strPattern).get(); } else { PatternCompiler pc = new Perl5Compiler(); try { Pattern pattern = pc.compile( strPattern, Perl5Compiler.CASE_INSENSITIVE_MASK | Perl5Compiler.READ_ONLY_MASK | Perl5Compiler.SINGLELINE_MASK); patterns.put(strPattern, new WeakReference<Pattern>(pattern)); return pattern; } catch (MalformedPatternException e) { LOG.error("puma pattern error."); return null; } } }
/** * Compile regexp parameter. * * @param name The name of the parameter. * @param value The parameter value. * @return Pattern[] The compiled patterns, or <code>null</code>. * @throws PluginException On malformed patterns. */ private Pattern[] compileGlobs(String name, String value) throws PluginException { Pattern[] result = null; if (null != value && 0 < value.length() && !STR_GLOBSTAR.equals(value)) { try { PatternCompiler pc = new GlobCompiler(); String[] ptrns = StringUtils.split(value, STR_COMMA); result = new Pattern[ptrns.length]; for (int n = 0; n < ptrns.length; n++) { result[n] = pc.compile(ptrns[n]); } } catch (MalformedPatternException e) { throw new PluginException( "Parameter " + name + " has a malformed pattern: " + e.getMessage()); } } return result; }
// FIXME: The compiled pattern strings should really be cached somehow. public void initialize(WikiContext context, Map params) throws PluginException { m_dateFormat = Preferences.getDateFormat(context, TimeFormat.DATETIME); m_engine = context.getEngine(); m_maxwidth = TextUtil.parseIntParameter((String) params.get(PARAM_MAXWIDTH), Integer.MAX_VALUE); if (m_maxwidth < 0) m_maxwidth = 0; String s = (String) params.get(PARAM_SEPARATOR); if (s != null) { m_separator = s; // pre-2.1.145 there was a separator at the end of the list // if they set the parameters, we use the new format of // before Item1 after separator before Item2 after separator before // Item3 after m_after = ""; } s = (String) params.get(PARAM_BEFORE); if (s != null) { m_before = s; } s = (String) params.get(PARAM_AFTER); if (s != null) { m_after = s; } s = (String) params.get(PARAM_EXCLUDE); if (s != null) { try { PatternCompiler pc = new GlobCompiler(); String[] ptrns = StringUtils.split(s, ","); m_exclude = new Pattern[ptrns.length]; for (int i = 0; i < ptrns.length; i++) { m_exclude[i] = pc.compile(ptrns[i]); } } catch (MalformedPatternException e) { throw new PluginException("Exclude-parameter has a malformed pattern: " + e.getMessage()); } } // TODO: Cut-n-paste, refactor s = (String) params.get(PARAM_INCLUDE); if (s != null) { try { PatternCompiler pc = new GlobCompiler(); String[] ptrns = StringUtils.split(s, ","); m_include = new Pattern[ptrns.length]; for (int i = 0; i < ptrns.length; i++) { m_include[i] = pc.compile(ptrns[i]); } } catch (MalformedPatternException e) { throw new PluginException("Include-parameter has a malformed pattern: " + e.getMessage()); } } // log.debug( "Requested maximum width is "+m_maxwidth ); s = (String) params.get(PARAM_SHOW); if (s != null) { if (s.equalsIgnoreCase("count")) { m_show = "count"; } } s = (String) params.get(PARAM_LASTMODIFIED); if (s != null) { if (s.equalsIgnoreCase("true")) { if (m_show.equals("count")) { m_lastModified = true; } else { throw new PluginException( "showLastModified=true is only valid if show=count is also specified"); } } } initSorter(context, params); }
public String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException { try { searchPattern = compiler.compile(((CompoundVariable) values[0]).execute()); generateTemplate(((CompoundVariable) values[1]).execute()); if (values.length > 2) { valueIndex = ((CompoundVariable) values[2]).execute(); } if (valueIndex.equals("")) { valueIndex = "1"; } if (values.length > 3) { between = ((CompoundVariable) values[3]).execute(); } if (values.length > 4) { String dv = ((CompoundVariable) values[4]).execute(); if (!dv.equals("")) { defaultValue = dv; } } if (values.length > 5) { name = ((CompoundVariable) values[values.length - 1]).execute(); } } catch (MalformedPatternException e) { log.error("", e); throw new InvalidVariableException("Bad regex pattern"); } catch (Exception e) { throw new InvalidVariableException(e.getMessage()); } getVariables().put(name, defaultValue); if (previousResult == null || previousResult.getResponseData() == null) { return defaultValue; } List collectAllMatches = new ArrayList(); try { PatternMatcher matcher = (PatternMatcher) localMatcher.get(); String responseText = new String(previousResult.getResponseData()); PatternMatcherInput input = new PatternMatcherInput(responseText); while (matcher.contains(input, searchPattern)) { MatchResult match = matcher.getMatch(); collectAllMatches.add(match); } } catch (NumberFormatException e) { log.error("", e); return defaultValue; } catch (Exception e) { return defaultValue; } if (collectAllMatches.size() == 0) { return defaultValue; } if (valueIndex.equals(ALL)) { StringBuffer value = new StringBuffer(); Iterator it = collectAllMatches.iterator(); boolean first = true; while (it.hasNext()) { if (!first) { value.append(between); } else { first = false; } value.append(generateResult((MatchResult) it.next())); } return value.toString(); } else if (valueIndex.equals(RAND)) { MatchResult result = (MatchResult) collectAllMatches.get(rand.nextInt(collectAllMatches.size())); return generateResult(result); } else { try { int index = Integer.parseInt(valueIndex) - 1; MatchResult result = (MatchResult) collectAllMatches.get(index); return generateResult(result); } catch (NumberFormatException e) { float ratio = Float.parseFloat(valueIndex); MatchResult result = (MatchResult) collectAllMatches.get((int) (collectAllMatches.size() * ratio + .5) - 1); return generateResult(result); } catch (IndexOutOfBoundsException e) { return defaultValue; } } }
public Regexp(final String pattern) throws MalformedPatternException { synchronized (COMPILER) { this.pattern = COMPILER.compile(pattern); } }