Example usage for java.util.regex Matcher groupCount

List of usage examples for java.util.regex Matcher groupCount

Introduction

In this page you can find the example usage for java.util.regex Matcher groupCount.

Prototype

public int groupCount() 

Source Link

Document

Returns the number of capturing groups in this matcher's pattern.

Usage

From source file:com.aliyun.datahub.flume.sink.serializer.OdpsRegexEventSerializer.java

@Override
public Map<String, String> getRow() throws UnsupportedEncodingException {
    Map<String, String> rowMap = Maps.newHashMap();
    Matcher m = inputPattern.matcher(new String(payload, charset));
    if (!m.matches()) {
        logger.debug("line not match regex!");
        return Maps.newHashMap();
    }/*  w w  w .j  a  v  a 2 s. c om*/

    if (m.groupCount() != inputColNames.length) {
        logger.debug("regex group num not match column num!");
        return Maps.newHashMap();
    }

    for (int i = 0; i < inputColNames.length; i++) {
        if (!StringUtils.isEmpty(inputColNames[i])) {
            rowMap.put(inputColNames[i], m.group(i + 1));
        }
    }

    return rowMap;
}

From source file:org.alinous.ftp.FtpManager.java

public String pwd(String sessionId) throws IOException {
    FTPClient ftp = this.instances.get(sessionId).getFtp();
    ftp.pwd();/*from   ww w. j a  va 2  s. c o m*/
    String cur = ftp.getReplyString();

    Matcher match = PWD_REPLY_MESSAGE.matcher(cur);
    if (!match.find() || match.groupCount() != 1) {
        return null;
    }
    return match.group(1);
}

From source file:com.falcon.orca.domain.DynDataStore.java

public String fillURLWithData() throws JsonProcessingException {
    Map<String, Object> scope = getUrlDataMap();
    StringWriter stringWriter = new StringWriter();
    urlMustache.execute(stringWriter, scope);
    String url = stringWriter.toString().trim();
    Matcher m = pattern.matcher(url);
    if (m.find()) {
        int groupCount = m.groupCount();
        while (groupCount > 0) {
            String keyToReplace = m.group(groupCount);
            if (dataGenerators.containsKey(keyToReplace)) {
                url = url.replace("@@" + keyToReplace + "@@",
                        String.valueOf(dataGenerators.get(m.group(groupCount)).next()));
            }/*from w  w w .  j  av  a 2s . co m*/
            groupCount--;
        }
    }
    return url;
}

From source file:com.falcon.orca.domain.DynDataStore.java

public String fillTemplateWithData() throws JsonProcessingException {
    Map<String, Object> scopes = getDataMap();
    StringWriter stringWriter = new StringWriter();
    bodyMustache.execute(stringWriter, scopes);
    String data = stringWriter.toString().trim();
    Matcher m = pattern.matcher(data);
    if (m.find()) {
        int groupCount = m.groupCount();
        while (groupCount > 0) {
            String keyToReplace = m.group(groupCount);
            if (dataGenerators.containsKey(keyToReplace)) {
                data = data.replace("@@" + keyToReplace + "@@",
                        String.valueOf(dataGenerators.get(m.group(groupCount)).next()));
            }// w  w w.j  av  a2  s . c o  m
            groupCount--;
        }
    }
    return data;
}

From source file:com.mozilla.pig.load.RegExLoader.java

@Override
public Tuple getNext() throws IOException {
    Tuple t = null;/* www.  jav a2s  .  com*/
    boolean tryNext = true;
    while (tryNext && reader.nextKeyValue()) {
        Text val = reader.getCurrentValue();
        if (val != null) {
            String line = val.toString();
            if (line.length() > 0 && line.charAt(line.length() - 1) == '\r') {
                line = line.substring(0, line.length() - 1);
            }
            Matcher m = getPattern().matcher(line);
            if (m.find()) {
                tryNext = false;
                t = TupleFactory.getInstance().newTuple();
                for (int i = 1; i <= m.groupCount(); i++) {
                    t.append(new DataByteArray(m.group(i)));
                }
            } else {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Failed to match line: " + val.toString());
                }
            }
        }
    }

    return t;
}

From source file:com.thero.framework.util.AntPathStringMatcher.java

/**
 * Main entry point.//from w  ww .j a va2s.c  o m
 *
 * @return <code>true</code> if the string matches against the pattern, or <code>false</code> otherwise.
 */
public boolean matchStrings() {
    Matcher matcher = pattern.matcher(str);
    if (matcher.matches()) {
        if (uriTemplateVariables != null) {
            // SPR-8455
            Assert.isTrue(variableNames.size() == matcher.groupCount(),
                    "The number of capturing groups in the pattern segment " + pattern
                            + " does not match the number of URI template variables it defines, which can occur if "
                            + " capturing groups are used in a URI template regex. Use non-capturing groups instead.");
            for (int i = 1; i <= matcher.groupCount(); i++) {
                String name = this.variableNames.get(i - 1);
                String value = matcher.group(i);
                uriTemplateVariables.put(name, value);
            }
        }
        return true;
    } else {
        return false;
    }
}

From source file:org.brekka.pegasus.core.services.impl.CertificateAuthenticationServiceImpl.java

@Override
@Transactional()/* w ww  .j a v  a 2s  . c o  m*/
public DigitalCertificate authenticate(X509Certificate certificate)
        throws BadCredentialsException, DisabledException {
    byte[] signature = certificate.getSignature();
    String subjectDN = certificate.getSubjectDN().getName();
    String commonName = null;

    Matcher matcher = matchAllowedSubjectDN(subjectDN, allowedSubjectDistinguishedNamePatterns);
    if (matcher.groupCount() > 0) {
        commonName = matcher.group(1);
    }

    byte[] subjectDNBytes = subjectDN.getBytes(Charset.forName("UTF-8"));
    SystemDerivedKeySpecType spec = config.getSubjectDerivedKeySpec();

    DerivedKey derivedKey = derivedKeyCryptoService.apply(subjectDNBytes, spec.getSalt(), null,
            CryptoProfile.Static.of(spec.getCryptoProfile()));
    byte[] distinguishedNameDigest = derivedKey.getDerivedKey();
    CertificateSubject certificateSubject = certificateSubjectDAO
            .retrieveByDistinguishedNameDigest(distinguishedNameDigest);
    if (certificateSubject == null) {
        // Create it
        certificateSubject = new CertificateSubject();
        certificateSubject.setDistinguishedNameDigest(distinguishedNameDigest);
        certificateSubjectDAO.create(certificateSubject);
    }

    DigitalCertificate digitalCertificate = digitalCertificateDAO
            .retrieveBySubjectAndSignature(certificateSubject, signature);
    if (digitalCertificate == null) {
        digitalCertificate = new DigitalCertificate();
        digitalCertificate.setActive(Boolean.TRUE);
        digitalCertificate.setCertificateSubject(certificateSubject);
        digitalCertificate.setCreated(certificate.getNotBefore());
        digitalCertificate.setExpires(certificate.getNotAfter());
        digitalCertificate.setSignature(signature);
        digitalCertificateDAO.create(digitalCertificate);
    }

    // Perform some checks
    if (BooleanUtils.isNotTrue(digitalCertificate.getActive())) {
        throw new DisabledException(
                String.format("The certficate with id '%s' has been disabled", digitalCertificate.getId()));
    }
    if (digitalCertificate.getExpires().before(new Date())) {
        throw new CredentialsExpiredException(String.format("The certficate with id '%s' expired %tF",
                digitalCertificate.getId(), digitalCertificate.getExpires()));
    }

    // Both of these are transient
    certificateSubject.setCommonName(commonName);
    certificateSubject.setDistinguishedName(subjectDN);
    return digitalCertificate;
}

From source file:edu.cornell.mannlib.vitro.webapp.controller.OntologyController.java

private String checkForRedirect(String url, String acceptHeader) {
    ContentType c = checkForLinkedDataRequest(url, acceptHeader);
    Matcher m = URI_PATTERN.matcher(url);
    if (m.matches() && m.groupCount() <= 2) {
        String group2 = "";

        if (m.group(2).indexOf(".") != -1) {
            group2 = m.group(2).substring(0, m.group(2).indexOf("."));
            System.out.println("group2 " + group2);
            System.out.println("group1 " + m.group(1));
        }//from w  w  w .java  2s .c  o  m

        if (c != null && !group2.trim().equals(m.group(1).trim())) {
            String redirectUrl = null;
            if (m.group(2).isEmpty() || m.group(2) == null) {
                redirectUrl = "/ontology/" + m.group(1) + "/" + m.group(1);
            } else {
                redirectUrl = "/ontology/" + m.group(1) + "/" + m.group(2) + "/" + m.group(2);
            }

            if (RDFXML_MIMETYPE.equals(c.getMediaType())) {
                return redirectUrl + ".rdf";
            } else if (N3_MIMETYPE.equals(c.getMediaType())) {
                return redirectUrl + ".n3";
            } else if (TTL_MIMETYPE.equals(c.getMediaType())) {
                return redirectUrl + ".ttl";
            } //else send them to html                                       
        }
        //else redirect to HTML representation
        return null;
    } else {
        return null;
    }
}

From source file:org.codhaus.groovy.grails.validation.routines.RegexValidator.java

/**
 * Validate a value against the set of regular expressions
 * returning the array of matched groups.
 *
 * @param value The value to validate./*from   ww w.j  av  a  2  s .  c om*/
 * @return String array of the <i>groups</i> matched if
 * valid or <code>null</code> if invalid
 */
public String[] match(String value) {
    if (value == null) {
        return null;
    }

    for (int i = 0; i < patterns.length; i++) {
        Matcher matcher = patterns[i].matcher(value);
        if (matcher.matches()) {
            int count = matcher.groupCount();
            String[] groups = new String[count];
            for (int j = 0; j < count; j++) {
                groups[j] = matcher.group(j + 1);
            }
            return groups;
        }
    }

    return null;
}

From source file:com.astamuse.asta4d.web.copyleft.SpringAntPathStringMatcher.java

/**
 * Main entry point./*from   www .  ja v  a  2 s  .  com*/
 * 
 * @return <code>true</code> if the string matches against the pattern, or
 *         <code>false</code> otherwise.
 */
public boolean matchStrings() {
    Matcher matcher = pattern.matcher(str);
    if (matcher.matches()) {
        if (uriTemplateVariables != null) {
            // SPR-8455
            Validate.isTrue(variableNames.size() == matcher.groupCount(),
                    "The number of capturing groups in the pattern segment " + pattern
                            + " does not match the number of URI template variables it defines, which can occur if "
                            + " capturing groups are used in a URI template regex. Use non-capturing groups instead.");
            for (int i = 1; i <= matcher.groupCount(); i++) {
                String name = this.variableNames.get(i - 1);
                String value = matcher.group(i);
                uriTemplateVariables.put(name, value);
            }
        }
        return true;
    } else {
        return false;
    }
}