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

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

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

Matcher.group介绍

[英]Returns the text that matched the whole regular expression.
[中]返回与整个正则表达式匹配的文本。

代码示例

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

private static boolean matches(String keyName, int major, int minor) {
  Matcher m = VERSION_PATTERN.matcher(keyName);
  if(m.matches()) {
    int mj = Integer.parseInt(m.group(1));
    if(mj>=major) {
      int mn = Integer.parseInt(m.group(2));
      if(mn>=minor)
        return true;
    }
  }
  return false;
}

代码示例来源:origin: org.mockito/mockito-core

static boolean isJava8BelowUpdate45(String jvmVersion) {
  Matcher matcher = JAVA_8_RELEASE_VERSION_SCHEME.matcher(jvmVersion);
  if (matcher.matches()) {
    int update = Integer.parseInt(matcher.group(1));
    return update < 45;
  }
  matcher = JAVA_8_DEV_VERSION_SCHEME.matcher(jvmVersion);
  if (matcher.matches()) {
    int update = Integer.parseInt(matcher.group(1));
    return update < 45;
  }
  matcher = Pattern.compile("1\\.8\\.0-b\\d+").matcher(jvmVersion);
  return matcher.matches();
}

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

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

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

String time = "14:35:59.99";
String timeRegex = "([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])(?:\\.([0-9]{1,3}))?";
Pattern pattern = Pattern.compile(timeRegex);
Matcher matcher = pattern.matcher(time);
if (matcher.matches()) {
  String hours = matcher.group(1);
  String minutes = matcher.group(2);
  String seconds = matcher.group(3);
  String miliSeconds = matcher.group(4);
  System.out.println(hours + ", " + minutes  + ", " + seconds + ", " + miliSeconds);
}

代码示例来源: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: spring-projects/spring-framework

@Override
public String extractVersion(String requestPath) {
  Matcher matcher = pattern.matcher(requestPath);
  if (matcher.find()) {
    String match = matcher.group(1);
    return (match.contains("-") ? match.substring(match.lastIndexOf('-') + 1) : match);
  }
  else {
    return null;
  }
}

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

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

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

private static String resolveJarUrl(Class<?> clazz) {
    URL location = clazz.getResource('/' + clazz.getName().replace('.', '/') + ".class");
    if (location != null) {
      Matcher matcher = Pattern.compile("(jar:file.*-[\\d.]+(-rc[\\d]+|-SNAPSHOT)?.jar)!.*$").matcher(location.toString());
      if (matcher.matches()) {
        return matcher.group(1);
      }
    }
    return null;
  }
}

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

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

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

@Override
@Nullable
public String extractVersion(String requestPath) {
  Matcher matcher = pattern.matcher(requestPath);
  if (matcher.find()) {
    String match = matcher.group(1);
    return (match.contains("-") ? match.substring(match.lastIndexOf('-') + 1) : match);
  }
  else {
    return null;
  }
}

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

public static String parseRevisionFromSvnInfo(String svnInfo) {
  String s = svnInfo.replaceAll("\\s", " ");
  Pattern pattern = Pattern.compile(".*revision=\"(\\d+)\".*");
  Matcher matcher = pattern.matcher(s);
  if (matcher.matches()) {
    return matcher.group(1);
  }
  throw bomb("Can not parse revision from svninfo: \n" + svnInfo);
}

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

/**
 * parse key-value pair.
 *
 * @param str           string.
 * @param itemSeparator item separator.
 * @return key-value map;
 */
private static Map<String, String> parseKeyValuePair(String str, String itemSeparator) {
  String[] tmp = str.split(itemSeparator);
  Map<String, String> map = new HashMap<String, String>(tmp.length);
  for (int i = 0; i < tmp.length; i++) {
    Matcher matcher = KVP_PATTERN.matcher(tmp[i]);
    if (!matcher.matches()) {
      continue;
    }
    map.put(matcher.group(1), matcher.group(2));
  }
  return map;
}

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

/**
 * Checks to see if the supplied {@code String} has any placeholders
 * that are not specified as constants on this class and throws an
 * {@code IllegalArgumentException} if so.
 */
private void checkForInvalidPlaceholders(String message) throws IllegalArgumentException {
  Matcher matcher = PATTERN.matcher(message);
  while (matcher.find()) {
    String match = matcher.group();
    if (!ALLOWED_PLACEHOLDERS.contains(match)) {
      throw new IllegalArgumentException("Placeholder [" + match + "] is not valid");
    }
  }
}

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

String mydata = "some string with 'the data i want' inside";
Pattern pattern = Pattern.compile("'(.*?)'");
Matcher matcher = pattern.matcher(mydata);
if (matcher.find())
{
  System.out.println(matcher.group(1));
}

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

private void handleAppsDelete(HttpExchange httpExchange) throws IOException {
  EurekaHttpResponse<?> httpResponse;
  String path = httpExchange.getRequestURI().getPath();
  Matcher matcher;
  if ((matcher = Pattern.compile("/v2/apps/([^/]+)/([^/]+)").matcher(path)).matches()) {
    httpResponse = requestHandler.cancel(matcher.group(1), matcher.group(2));
  } else if ((matcher = Pattern.compile("/v2/apps/([^/]+)/([^/]+)/status").matcher(path)).matches()) {
    httpResponse = requestHandler.deleteStatusOverride(matcher.group(1), matcher.group(2), null);
  } else {
    httpExchange.sendResponseHeaders(HttpServletResponse.SC_NOT_FOUND, 0);
    return;
  }
  mapResponse(httpExchange, httpResponse);
}

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

/**
 * parse key-value pair.
 *
 * @param str           string.
 * @param itemSeparator item separator.
 * @return key-value map;
 */
private static Map<String, String> parseKeyValuePair(String str, String itemSeparator) {
  String[] tmp = str.split(itemSeparator);
  Map<String, String> map = new HashMap<String, String>(tmp.length);
  for (int i = 0; i < tmp.length; i++) {
    Matcher matcher = KVP_PATTERN.matcher(tmp[i]);
    if (!matcher.matches()) {
      continue;
    }
    map.put(matcher.group(1), matcher.group(2));
  }
  return map;
}

相关文章