java.util.regex.Matcher.toMatchResult()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(8.2k)|赞(0)|评价(0)|浏览(104)

本文整理了Java中java.util.regex.Matcher.toMatchResult()方法的一些代码示例,展示了Matcher.toMatchResult()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Matcher.toMatchResult()方法的具体详情如下:
包路径:java.util.regex.Matcher
类名称:Matcher
方法名:toMatchResult

Matcher.toMatchResult介绍

[英]Converts the current match into a separate MatchResult instance that is independent from this matcher. The new object is unaffected when the state of this matcher changes.
[中]将当前匹配转换为独立于此匹配器的单独MatchResult实例。此匹配器的状态更改时,新对象不受影响。

代码示例

代码示例来源:origin: google/guava

/**
 * 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();
}

代码示例来源:origin: stanfordnlp/CoreNLP

public Object matchWithResult(String str) {
 if (str == null) return null;
 Matcher m = pattern.matcher(str);
 if (m.matches()) {
  return m.toMatchResult();
 } else {
  return null;
 }
}

代码示例来源:origin: robovm/robovm

/**
 * 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();
}

代码示例来源:origin: apache/hive

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 "";
}

代码示例来源:origin: apache/drill

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 "";
}

代码示例来源:origin: cSploit/android

/**
  * 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;
 }
}

代码示例来源:origin: stackoverflow.com

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());
    }
  }
}

代码示例来源:origin: Graylog2/graylog2-server

@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);
}

代码示例来源:origin: stackoverflow.com

pending = matcher.toMatchResult();

代码示例来源:origin: languagetool-org/languagetool

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();

代码示例来源:origin: stackoverflow.com

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);
    }
  }
}

代码示例来源:origin: apache/flume

continue;
MatchResult res = matcher.toMatchResult();
for (int grp = 1; grp <= res.groupCount(); grp++) {
 String value = res.group(grp);

代码示例来源:origin: apache/nifi

final MatchResult res = matcher.toMatchResult();
for (int grp = 1; grp <= res.groupCount(); grp++) {
  String value = res.group(grp);

代码示例来源:origin: commons-net/commons-net

/**
 * 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;
}

代码示例来源:origin: webx/citrus

private static MatchResult createEmptyMatchResult() {
  Matcher matcher = Pattern.compile("^$").matcher("");
  assertTrue(matcher.find());
  return matcher.toMatchResult();
}

代码示例来源:origin: webx/citrus

private static MatchResult createEmptyMatchResult() {
  Matcher matcher = Pattern.compile("^$").matcher("");
  assertTrue(matcher.find());
  return matcher.toMatchResult();
}

代码示例来源:origin: webx/citrus

private static MatchResult createEmptyMatchResult() {
  Matcher matcher = Pattern.compile("^$").matcher("");
  assertTrue(matcher.find());
  return matcher.toMatchResult();
}

代码示例来源:origin: com.liferay.portal/com.liferay.portal.kernel

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();
}

代码示例来源:origin: vsch/flexmark-java

/**
 * 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;
  }
}

代码示例来源:origin: webx/citrus

/**
 * 试图匹配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;
}

相关文章