/** * Returns a new {@code MatchResult} that corresponds to a successful match. Apache Harmony (used * in Android) requires a successful match in order to generate a {@code MatchResult}: * http://goo.gl/5VQFmC */ private static MatchResult newMatchResult() { Matcher matcher = Pattern.compile(".").matcher("X"); matcher.find(); return matcher.toMatchResult(); }
public Object matchWithResult(String str) { if (str == null) return null; Matcher m = pattern.matcher(str); if (m.matches()) { return m.toMatchResult(); } else { return null; } }
/** * Returns the result of the last matching operation. * <p> * The next* and find* methods return the match result in the case of a * successful match. * * @return the match result of the last successful match operation * @throws IllegalStateException * if the match result is not available, of if the last match * was not successful. */ public MatchResult match() { if (!matchSuccessful) { throw new IllegalStateException(); } return matcher.toMatchResult(); }
public String evaluate(String s, String regex, Integer extractIndex) { if (s == null || regex == null) { return null; } if (!regex.equals(lastRegex) || p == null) { lastRegex = regex; p = Pattern.compile(regex); } Matcher m = p.matcher(s); if (m.find()) { MatchResult mr = m.toMatchResult(); return mr.group(extractIndex); } return ""; }
public String evaluate(String s, String regex, Integer extractIndex) { if (s == null || regex == null) { return null; } if (!regex.equals(lastRegex) || p == null) { lastRegex = regex; p = Pattern.compile(regex); } Matcher m = p.matcher(s); if (m.find()) { MatchResult mr = m.toMatchResult(); return mr.group(extractIndex); } return ""; }
/** * extract the charset encoding of a web site from the <meta> headers. * * @param body html body of the site to be parsed * @return returns the charset encoding if we've found it, or null. */ public static String getCharsetFromBody(String body) { if (body != null) { int headEnd = body.toLowerCase().trim().indexOf("</head>"); // return null if there's no head tags if (headEnd == -1) return null; String body_head = body.toLowerCase().substring(0, headEnd); Pattern p = Pattern.compile("charset=([\"\'a-z0-9A-Z-]+)"); Matcher m = p.matcher(body_head); String str_match = ""; if (m.find()) { str_match = m.toMatchResult().group(1); return str_match.replaceAll("[\"']", ""); } } return null; } }
public class CallbackMatcher { public static interface Callback { public void foundMatch(MatchResult matchResult); } private final Pattern pattern; public CallbackMatcher(String regex) { this.pattern = Pattern.compile(regex); } public String findMatches(String string, Callback callback) { final Matcher matcher = this.pattern.matcher(string); while(matcher.find()) { callback.foundMatch(matcher.toMatchResult()); } } }
@Override public RegexMatchResult evaluate(FunctionArgs args, EvaluationContext context) { final Pattern regex = pattern.required(args, context); final String value = this.value.required(args, context); if (regex == null || value == null) { final String nullArgument = regex == null ? "pattern" : "value"; throw new IllegalArgumentException("Argument '" + nullArgument + "' cannot be 'null'"); } //noinspection unchecked final List<String> groupNames = (List<String>) optionalGroupNames.optional(args, context).orElse(Collections.emptyList()); final Matcher matcher = regex.matcher(value); final boolean matches = matcher.find(); return new RegexMatchResult(matches, matcher.toMatchResult(), groupNames); }
pending = matcher.toMatchResult();
Matcher matcher = specialPattern.matcher(sentence.getText()); while (matcher.find()) { MatchResult result = matcher.toMatchResult(); Double value = specialPatterns.get(specialPattern).getValue().apply(result); Unit unit = specialPatterns.get(specialPattern).getKey();
import java.util.regex.*; public class CallbackMatcher { public static interface Callback { public String foundMatch(MatchResult matchResult); } private final Pattern pattern; public CallbackMatcher(String regex) { this.pattern = Pattern.compile(regex); } public String replaceMatches(String string, Callback callback) { final Matcher matcher = this.pattern.matcher(string); while(matcher.find()) { final MatchResult matchResult = matcher.toMatchResult(); final String replacement = callback.foundMatch(matchResult); string = string.substring(0, matchResult.start()) + replacement + string.substring(matchResult.end()); matcher.reset(string); } } }
continue; MatchResult res = matcher.toMatchResult(); for (int grp = 1; grp <= res.groupCount(); grp++) { String value = res.group(grp);
final MatchResult res = matcher.toMatchResult(); for (int grp = 1; grp <= res.groupCount(); grp++) { String value = res.group(grp);
/** * Convenience method delegates to the internal MatchResult's matches() * method. * * @param s the String to be matched * @return true if s matches this object's regular expression. */ public boolean matches(String s) { this.result = null; _matcher_ = pattern.matcher(s); if (_matcher_.matches()) { this.result = _matcher_.toMatchResult(); } return null != this.result; }
private static MatchResult createEmptyMatchResult() { Matcher matcher = Pattern.compile("^$").matcher(""); assertTrue(matcher.find()); return matcher.toMatchResult(); }
private static MatchResult createEmptyMatchResult() { Matcher matcher = Pattern.compile("^$").matcher(""); assertTrue(matcher.find()); return matcher.toMatchResult(); }
private static MatchResult createEmptyMatchResult() { Matcher matcher = Pattern.compile("^$").matcher(""); assertTrue(matcher.find()); return matcher.toMatchResult(); }
public String replaceMatches(CharSequence charSequence, Callback callback) { Matcher matcher = _pattern.matcher(charSequence); StringBuilder sb = new StringBuilder(charSequence); int offset = 0; while (matcher.find()) { MatchResult matchResult = matcher.toMatchResult(); String replacement = callback.foundMatch(matchResult); if (replacement == null) { continue; } int matchStart = offset + matchResult.start(); int matchEnd = offset + matchResult.end(); sb.replace(matchStart, matchEnd, replacement); int matchLength = matchResult.end() - matchResult.start(); int lengthChange = replacement.length() - matchLength; offset += lengthChange; } return sb.toString(); }
/** * If RE matches at current index in the input, advance index and return the match; otherwise return null. * * @param re pattern to match * @return sequence matched or null */ @Override public BasedSequence match(Pattern re) { if (index >= input.length()) { return null; } Matcher matcher = re.matcher(input); matcher.region(index, input.length()); boolean m = matcher.find(); if (m) { index = matcher.end(); MatchResult result = matcher.toMatchResult(); return input.subSequence(result.start(), result.end()); } else { return null; } }
/** * 试图匹配rule。 * <p> * 如果匹配,则返回匹配结果。否则返回<code>null</code>表示不匹配。 * </p> */ public MatchResult match(String path) { Matcher matcher = pattern.matcher(path); boolean matched = matcher.find(); if (!negative && matched) { if (log.isDebugEnabled()) { log.debug("Testing \"{}\" with rule pattern: \"{}\", MATCHED", StringEscapeUtil.escapeJava(path), StringEscapeUtil.escapeJava(patternString)); } return matcher.toMatchResult(); } if (negative && !matched) { if (log.isDebugEnabled()) { log.debug("Testing \"{}\" with rule pattern: \"{}\", MATCHED", StringEscapeUtil.escapeJava(path), StringEscapeUtil.escapeJava(patternString)); } return MatchResultSubstitution.EMPTY_MATCH_RESULT; } if (log.isTraceEnabled()) { log.trace("Testing \"{}\" with rule pattern: \"{}\", MISMATCHED", StringEscapeUtil.escapeJava(path), StringEscapeUtil.escapeJava(patternString)); } return null; }