java.util.regex.Matcher类的使用及代码示例

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

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

Matcher介绍

[英]The result of applying a Pattern to a given input. See Pattern for example uses.
[中]将模式应用于给定输入的结果。请参阅模式以了解使用的示例。

代码示例

代码示例来源:origin: spring-projects/spring-framework

/**
 * Returns {@code true} if the exclusion {@link Pattern} at index {@code patternIndex}
 * matches the supplied candidate {@code String}.
 */
@Override
protected boolean matchesExclusion(String candidate, int patternIndex) {
  Matcher matcher = this.compiledExclusionPatterns[patternIndex].matcher(candidate);
  return matcher.matches();
}

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

private String getError (String line) {
  Pattern pattern = Pattern.compile(":[0-9]+:[0-9]+:(.+)");
  Matcher matcher = pattern.matcher(line);
  matcher.find();
  return matcher.groupCount() >= 1 ? matcher.group(1).trim() : null;
}

代码示例来源:origin: spring-projects/spring-framework

/**
   * Returns the length of the given pattern, where template variables are considered to be 1 long.
   */
  public int getLength() {
    if (this.length == null) {
      this.length = (this.pattern != null ?
          VARIABLE_PATTERN.matcher(this.pattern).replaceAll("#").length() : 0);
    }
    return this.length;
  }
}

代码示例来源:origin: jenkinsci/jenkins

public SubText(Matcher m, int textOffset) {
  start = m.start() + textOffset;
  end   = m.end() + textOffset;
  int cnt = m.groupCount();
  groups = new int[cnt*2];
  for( int i=0; i<cnt; i++ ) {
    groups[i*2  ] = m.start(i+1) + textOffset;
    groups[i*2+1] = m.end(i+1) + textOffset;
  }
}

代码示例来源: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: square/retrofit

/**
 * Gets the set of unique path parameters used in the given URI. If a parameter is used twice
 * in the URI, it will only show up once in the set.
 */
static Set<String> parsePathParameters(String path) {
 Matcher m = PARAM_URL_REGEX.matcher(path);
 Set<String> patterns = new LinkedHashSet<>();
 while (m.find()) {
  patterns.add(m.group(1));
 }
 return patterns;
}

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

/**
 * Extracts the hostname from a "host:port" address string.
 * @param address address string to parse
 * @return hostname or null if the given address is incorrect
 */
public static String getHost(String address) {
  Matcher matcher = HOST_PORT_PATTERN.matcher(address);
  return matcher.matches() ? matcher.group(1) : null;
}

代码示例来源:origin: perwendel/spark

protected final String[] parseKey(String key) {
  Matcher m = KEY_PATTERN.matcher(key);
  if (m.find()) {
    return new String[] {cleanKey(m.group()), key.substring(m.end())};
  } else {
    return null; // NOSONAR
  }
}

代码示例来源:origin: apache/incubator-druid

protected static String sanitize(String namespace)
 {
  Pattern DOT_OR_WHITESPACE = Pattern.compile("[\\s]+|[.]+");
  return DOT_OR_WHITESPACE.matcher(namespace).replaceAll("_");
 }
}

代码示例来源:origin: shuzheng/zheng

/**
 * 驼峰转下划线,效率比上面高
 * @param str
 * @return
 */
public static String humpToLine(String str) {
  Matcher matcher = humpPattern.matcher(str);
  StringBuffer sb = new StringBuffer();
  while (matcher.find()) {
    matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
  }
  matcher.appendTail(sb);
  return sb.toString();
}

代码示例来源:origin: eclipse-vertx/vert.x

public static boolean parseRotateOptionFromResolvConf(String s) {
  Matcher matcher = ROTATE_OPTIONS_PATTERN.matcher(s);
  return matcher.find();
 }
}

代码示例来源:origin: spring-projects/spring-framework

private void assertDescriptionContainsExpectedPath(ClassPathResource resource, String expectedPath) {
  Matcher matcher = DESCRIPTION_PATTERN.matcher(resource.getDescription());
  assertTrue(matcher.matches());
  assertEquals(1, matcher.groupCount());
  String match = matcher.group(1);
  assertEquals(expectedPath, match);
}

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

private static void assertContainsRegex(String expectedRegex, String actual) {
  Pattern pattern = Pattern.compile(expectedRegex);
  Matcher matcher = pattern.matcher(actual);
  if (!matcher.find()) {
   String actualDesc = (actual == null) ? "null" : ('<' + actual + '>');
   fail("expected to contain regex:<" + expectedRegex + "> but was:" + actualDesc);
  }
 }
}

代码示例来源:origin: Netflix/zuul

private static String cleanCookieHeader(String cookie)
{
  for (Pattern stripPtn : RE_STRIP) {
    Matcher matcher = stripPtn.matcher(cookie);
    if (matcher.find()) {
      cookie = matcher.replaceAll("");
    }
  }
  return cookie;
}

代码示例来源:origin: org.apache.commons/commons-lang3

@Override
boolean parse(final FastDateParser parser, final Calendar calendar, final String source, final ParsePosition pos, final int maxWidth) {
  final Matcher matcher = pattern.matcher(source.substring(pos.getIndex()));
  if (!matcher.lookingAt()) {
    pos.setErrorIndex(pos.getIndex());
    return false;
  }
  pos.setIndex(pos.getIndex() + matcher.end(1));
  setCalendar(parser, calendar, matcher.group(1));
  return true;
}

代码示例来源:origin: prestodb/presto

public BinaryLiteral(Optional<NodeLocation> location, String value)
{
  super(location);
  requireNonNull(value, "value is null");
  String hexString = WHITESPACE_PATTERN.matcher(value).replaceAll("").toUpperCase();
  if (NOT_HEX_DIGIT_PATTERN.matcher(hexString).matches()) {
    throw new ParsingException("Binary literal can only contain hexadecimal digits", location.get());
  }
  if (hexString.length() % 2 != 0) {
    throw new ParsingException("Binary literal must contain an even number of digits", location.get());
  }
  this.value = Slices.wrappedBuffer(BaseEncoding.base16().decode(hexString));
}

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

private String escape(String jsonStringValue) {
  String replace1 = DOUBLEQUOTE.matcher(jsonStringValue).replaceAll(Matcher.quoteReplacement("\\\""));
  return BACKSLASH.matcher(replace1).replaceAll(Matcher.quoteReplacement("\\\\"));
}

代码示例来源:origin: square/retrofit

private void validatePathName(int p, String name) {
 if (!PARAM_NAME_REGEX.matcher(name).matches()) {
  throw parameterError(method, p, "@Path parameter name must match %s. Found: %s",
    PARAM_URL_REGEX.pattern(), name);
 }
 // Verify URL replacement name is actually present in the URL path.
 if (!relativeUrlParamNames.contains(name)) {
  throw parameterError(method, p, "URL \"%s\" does not contain \"{%s}\".", relativeUrl, name);
 }
}

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

@Override
public boolean matches() {
 return matcher.matches();
}

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

ConfigVariable(Matcher matcher) {
  this.providerName = matcher.group(1);
  this.path = matcher.group(3) != null ? matcher.group(3) : EMPTY_PATH;
  this.variable = matcher.group(4);
}

相关文章