Matcher groupCount - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Matcher groupCount method.

This code example shows how to use the following methods:find, group, groupCount
    public void testGroups() throws Exception { 
        Pattern p = Pattern.compile("(b)([c|d])(z*)");
        Matcher m = p.matcher("abcdefg");
 
        /* Must call find() first, otherwise group*() are undefined. */ 
        assertTrue(m.find());
 
        assertEquals(3, m.groupCount());
 
        assertEquals("bc", m.group(0));
        assertEquals("b", m.group(1));
        assertEquals("c", m.group(2));
        assertEquals("", m.group(3));
    } 
 
    public void testFind() throws Exception { 
        Pattern p = Pattern.compile(".");
        Matcher m = p.matcher("abc");
 
        assertTrue(m.find());
8
CodeRank
This code example shows how to use the following methods:group, groupCount, matches
        return load(null, href);
    } 
     
    public static FunctionLibrary load(File file, String href) {
        Matcher hrefMatcher = HREF_PATTERN.matcher(href);
        checkArgument(hrefMatcher.matches(), "Library identifier should be in the form language:filename.ext");
        checkState(hrefMatcher.groupCount() == 2);
        String language = hrefMatcher.group(1);
        String identifier = hrefMatcher.group(2);
        if (file != null)
            file = file.getParentFile();
        if (language.equals("java")) {
            return JavaLibrary.loadStaticClass(identifier);
        } else if (language.equals("clojure")) {
            return ClojureLibrary.loadScript(file, identifier);
        } else if (language.equals("python")) {
            return PythonLibrary.loadScript(file, identifier);
        } else { 
            throw new LoadException(file, "Unknown function library type " + language + ".");
        } 
8
CodeRank
This code example shows how to use the following methods:group, groupCount, matches
  public static long parseDuration(String durationStr, TimeUnit defaultUnit) throws InvalidConfigException
  { 
    if (null == durationStr) return 0;
    TimeUnit unit = defaultUnit;
    Matcher m = DURATION_PATTERN.matcher(durationStr);
    if (!m.matches()) throw new InvalidConfigException("invalid duration string: " + durationStr);
    if (1 < m.groupCount() && null != m.group(2) && 0 < m.group(2).length())
    { 
      char unitChar1 = m.group(2).charAt(0);
      switch (unitChar1)
      { 
      case 'n': 
      case 'N': unit = TimeUnit.NANOSECONDS; break;
      case 'u': 
      case 'U': unit = TimeUnit.MICROSECONDS; break;
      case 'm': 
      case 'M': 
        char unitChar3 = m.group(2).length() >= 3 ? m.group(2).charAt(2) : ' ';
        unit = ('n' == unitChar3 || 'N' == unitChar3) ? TimeUnit.MINUTES : TimeUnit.MILLISECONDS;
        break; 

8
CodeRank
This code example shows how to use the following methods:find, group, groupCount
  @Override 
  public int getPartitionId(URI uri) throws PartitionAccessException
  { 
    final String uriString = uri.toString();
    final Matcher matcher = _pattern.matcher(uriString);
    if (matcher.find())
    { 
      // When people supply the regex, ideally there should be only on matching group (wrapped with '()') 
      // which d2 would use as the partitioning key 
      final String key = matcher.group(matcher.groupCount());
      return getPartitionId(key);
    } 
    else 
    { 
      throw new PartitionAccessException("Pattern: " + _pattern.toString()
          + " does not match anything in request URI: " + uriString);
    } 
  } 
 
  @Override 
8
CodeRank
This code example shows how to use the following methods:group, groupCount
            throw new IllegalArgumentException(path + " is not a valid path");
        } 
    } 
 
    public String getKey() {
        return matcher.group(1);
    } 
 
    public String getDesignDocumentName() {
        return "_design/" + matcher.group(3);
    } 
 
    public String getDatabaseName() {
        return matcher.group(2);
    } 
 
    public String getViewName() {
        return matcher.group(4);
    } 
 

8
CodeRank
This code example shows how to use the following methods:group, groupCount, matches
        return moduleDependency;
    } 
 
    private ParsedModuleStringNotation splitModuleFromExtension(String notation) {
        Matcher matcher = EXTENSION_SPLITTER.matcher(notation);
        boolean hasArtifactType = matcher.matches();
        if (hasArtifactType && !ClientModule.class.isAssignableFrom(wantedType)) {
            if (matcher.groupCount() != 2) {
                throw new InvalidUserDataException("The dependency notation " + notation + " is invalid");
            } 
            return new ParsedModuleStringNotation(matcher.group(1), matcher.group(2));
        } 
        return new ParsedModuleStringNotation(notation, null);
    } 
} 
7
CodeRank
This code example shows how to use the following methods:find, group, groupCount
	} 
	 
	private void init(File publicKeyFile) throws OpenShiftException, FileNotFoundException, IOException {
		String keyWithIdAndComment = StreamUtils.readToString(new FileReader(publicKeyFile));
		Matcher matcher = PUBLICKEY_PATTERN.matcher(keyWithIdAndComment);
		if (!matcher.find()
				|| matcher.groupCount() < 1) {
			throw new OpenShiftException("Could not load public key from file \"{0}\"", publicKeyFile.getAbsolutePath());
		} 
 
		setKeyType(matcher.group(1));
		this.publicKey = matcher.group(2);
	} 
 
} 
7
CodeRank
This code example shows how to use the following methods:find, group, groupCount
        log.warning("Fixing missing namespace declaration for: " + missingNS);
 
        // Extract <root> attributes 
        pattern = Pattern.compile("<root([^>]*)");
        matcher = pattern.matcher(descriptorXml);
        if (!matcher.find() || matcher.groupCount() != 1) {
            log.fine("Could not find <root> element attributes");
            return null; 
        } 
 
        String rootAttributes = matcher.group(1);
        log.fine("Preserving existing <root> element attributes/namespace declarations: " + matcher.group(0));
 
        // Extract <root> body 
        pattern = Pattern.compile("<root[^>]*>(.*)</root>", Pattern.DOTALL);
        matcher = pattern.matcher(descriptorXml);
        if (!matcher.find() || matcher.groupCount() != 1) {
            log.fine("Could not extract body of <root> element");
            return null; 
        } 
7
CodeRank
This code example shows how to use the following methods:group, groupCount, matches
	private static final Pattern DESCRIPTION_PATTERN = Pattern.compile("^class path resource \\[(.+?)\\]$");
 
 
	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);
	} 
 
	private void assertExceptionContainsFullyQualifiedPath(ClassPathResource resource) {
		try { 
			resource.getInputStream();
			fail("FileNotFoundException expected for resource: " + resource);
		} 
		catch (IOException ex) {
			assertThat(ex, instanceOf(FileNotFoundException.class));
			assertThat(ex.getMessage(), containsString(FQ_RESOURCE_PATH));
7
CodeRank
This code example shows how to use the following methods:end, find, group, groupCount
        return null; 
    } 
 
    private Token each() { 
        Matcher matcher = scanner.getMatcherForPattern("^(?:- *)?(?:each|for) +(\\w+)(?: *, *(\\w+))? * in *([^\\n]+)");
        if (matcher.find(0) && matcher.groupCount() > 1) {
            consume(matcher.end());
            String value = matcher.group(1);
            String key = matcher.group(2);
            String code = matcher.group(3);
            Each each = new Each(value, lineno);
            each.setCode(code);
            each.setKey(key);
            return each;
        } 
        return null; 
        /* 
		 * if (captures = /^(?:- *)?(?:each|for) +(\w+)(?: *, *(\w+))? * in 
		 * *([^\n]+)/.exec(this.input)) { this.consume(captures[0].length); var 
		 * tok = this.tok('each', captures[1]); tok.key = captures[2] || 
7
CodeRank

Related Matcher groupCount Questions & Answers:

groupCount Method Overview
Returns the number of capturing groups in this matcher's pattern.
See Code Examples for other Java 8 Matcher Methods: