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:org.moserp.infrastructure.gateway.filter.ResponseLinksMapperTest.java

@Test
public void testRegExp() {
    Pattern serviceNamePattern = Pattern.compile("http[s]?://([a-zA-Z0-9\\-]+).*?");
    Matcher matcher = serviceNamePattern.matcher("http://service-name/service");
    assertTrue("Matches", matcher.matches());
    assertEquals("Group count", 1, matcher.groupCount());
    assertEquals("Group", "service-name", matcher.group(1));
}

From source file:com.haulmont.cuba.gui.exception.UniqueConstraintViolationHandler.java

protected boolean doHandle(Throwable throwable, WindowManager windowManager) {
    Pattern pattern = clientConfig.getUniqueConstraintViolationPattern();
    String constraintName = "";

    Matcher matcher = pattern.matcher(throwable.toString());
    if (matcher.find()) {
        if (matcher.groupCount() == 1) {
            constraintName = matcher.group(1);
        } else {/*from  w ww .  j a v a 2  s. c  o m*/
            for (int i = 1; i > matcher.groupCount(); i++) {
                if (StringUtils.isNotBlank(matcher.group(i))) {
                    constraintName = matcher.group(i);
                    break;
                }
            }
        }

        String msg = "";
        if (StringUtils.isNotBlank(constraintName)) {
            msg = messages.getMainMessage(constraintName.toUpperCase());
        }

        if (msg.equalsIgnoreCase(constraintName)) {
            msg = messages.getMainMessage("uniqueConstraintViolation.message");
            if (StringUtils.isNotBlank(constraintName)) {
                msg = msg + " (" + constraintName + ")";
            }
        }

        windowManager.showNotification(msg, Frame.NotificationType.ERROR);
        return true;
    }
    return false;
}

From source file:org.codelibs.fess.crawler.helper.RobotsTxtHelper.java

protected String getValue(final Pattern pattern, final String line) {
    final Matcher m = pattern.matcher(line);
    if (m.matches() && m.groupCount() > 0) {
        return m.group(1);
    }/*from  w  w w  .  j  av  a  2s.  c  om*/
    return null;
}

From source file:com.jaeksoft.searchlib.script.CommandAbstract.java

protected String findPatternFunction(int from, Pattern pattern) {
    String functionParam = null;/*  w  w w.j  a  va2s  .  c om*/
    for (int i = from; i < getParameterCount(); i++) {
        String param = getParameterString(i);
        if (StringUtils.isEmpty(param))
            continue;
        Matcher matcher = RegExpUtils.matcher(pattern, param);
        if (matcher.find())
            if (matcher.groupCount() > 0)
                functionParam = matcher.group(1);
    }
    return functionParam;
}

From source file:com.adobe.acs.commons.util.datadefinitions.impl.LocalizedTitleDefinitionBuilderImpl.java

@Override
public final ResourceDefinition convert(String data) {
    data = StringUtils.stripToEmpty(data);

    Map<String, String> localizedTitles = new TreeMap<String, String>();

    String name;/*from www  .j a  v  a  2 s  .co  m*/
    String title = "";

    final Matcher matcher = NODE_NAME_PATTERN.matcher(data);

    if (matcher.find() && matcher.groupCount() == 1) {
        name = StringUtils.stripToEmpty(matcher.group(1));
    } else {
        return null;
    }

    final String rawLocalizedTitles = StringUtils.stripToEmpty(NODE_NAME_PATTERN.matcher(data).replaceAll(""));
    final Matcher localeMatch = LOCALIZED_TITLES_PATTERN.matcher(rawLocalizedTitles);

    String firstLocale = null;
    while (localeMatch.find()) {
        final String locale = StringUtils.stripToEmpty(localeMatch.group(1));
        final String localeTitle = StringUtils.stripToEmpty(localeMatch.group(2));

        if (firstLocale == null) {
            firstLocale = locale;
        }

        if (DEFAULT_LOCALE_KEY.equals(locale)) {
            title = localeTitle;
        } else {
            localizedTitles.put(locale, localeTitle);
        }
    }

    // It no default title was found, fall back to the first locale listed

    if (StringUtils.isEmpty(title) && firstLocale != null) {
        title = localizedTitles.get(firstLocale);
    }

    if (StringUtils.isEmpty(title)) {
        return null;
    }

    final BasicResourceDefinition tagData = new BasicResourceDefinition(name);
    tagData.setTitle(title);
    tagData.setLocalizedTitles(localizedTitles);
    return tagData;
}

From source file:edu.cornell.mannlib.vitro.webapp.filters.PageRoutingFilter.java

@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain chain)
        throws IOException, ServletException {
    ServletContext ctx = filterConfig.getServletContext();

    PageDao pageDao = ModelAccess.on(ctx).getWebappDaoFactory().getPageDao();
    Map<String, String> urlMappings = pageDao.getPageMappings();

    // get URL without hostname or servlet context
    HttpServletResponse response = (HttpServletResponse) arg1;
    HttpServletRequest req = (HttpServletRequest) arg0;
    String path = req.getRequestURI().substring(req.getContextPath().length());

    // check for first part of path
    // ex. /hats/superHat -> /hats
    Matcher m = urlPartPattern.matcher(path);
    if (m.matches() && m.groupCount() >= 1) {
        String path1stPart = m.group(1);
        String pageUri = urlMappings.get(path1stPart);

        //try it with a leading slash?
        if (pageUri == null)
            pageUri = urlMappings.get("/" + path1stPart);

        if (pageUri != null && !pageUri.isEmpty()) {
            log.debug(path + "is a request to a page defined in the display model as " + pageUri);

            //add the pageUri to the request scope for use by the PageController
            PageController.putPageUri(req, pageUri);

            //This will send requests to HomePageController or PageController
            String controllerName = getControllerToForwardTo(req, pageUri, pageDao);
            log.debug(path + " is being forwarded to controller " + controllerName);

            RequestDispatcher rd = ctx.getNamedDispatcher(controllerName);
            if (rd == null) {
                log.error(path + " should be forwarded to controller " + controllerName + " but there "
                        + "is no servlet named that defined for the web application in web.xml");
                //TODO: what should be done in this case?
            }//  w w w .ja  v a 2 s .  co m

            rd.forward(req, response);
        } else if ("/".equals(path) || path.isEmpty()) {
            log.debug("url '" + path + "' is being forward to home controller");
            RequestDispatcher rd = ctx.getNamedDispatcher(HOME_CONTROLLER_NAME);
            rd.forward(req, response);
        } else {
            doNonDisplayPage(path, arg0, arg1, chain);
        }
    } else {
        doNonDisplayPage(path, arg0, arg1, chain);
    }
}

From source file:com.threewks.thundr.route.Route.java

public Map<String, String> getPathVars(String routePath) {
    Matcher matcher = routeMatchRegex.matcher(routePath);
    matcher.find();/*from  www.j av  a 2 s .  c o  m*/
    int count = matcher.groupCount();
    Map<String, String> results = new HashMap<String, String>();
    for (int i = 1; i <= count; i++) {
        String name = pathParameters.get(i - 1);

        results.put(name, URLEncoder.decodePathComponent(matcher.group(i)));
    }
    return results;
}

From source file:com.boozallen.cognition.ingest.storm.bolt.starter.LineRegexReplaceInRegionBolt.java

String replace(String record, Pattern pattern, String regex, String replacement) {
    Matcher match = pattern.matcher(record);
    if (match.find() && match.groupCount() > 0) {
        // only replace the first group
        int startPos = match.start(1);
        int stopPos = match.end(1);

        String replaceString = match.group(1).replaceAll(regex, replacement);
        return record.substring(0, startPos) + replaceString + record.substring(stopPos);
    } else {/*from   w  ww. ja va  2 s . c  om*/
        // no match, returns original
        return record;
    }
}

From source file:org.craftercms.studio.impl.v1.asset.processing.AbstractAssetProcessor.java

protected String getOutputRepoPath(ProcessorConfiguration config, Matcher inputPathMatcher) {
    if (StringUtils.isNotEmpty(config.getOutputPathFormat())) {
        int groupCount = inputPathMatcher.groupCount();
        String outputPath = config.getOutputPathFormat();

        for (int i = 1; i <= groupCount; i++) {
            outputPath = outputPath.replace("$" + i, inputPathMatcher.group(i));
        }/*from w  w w.j  av  a  2s.  c om*/

        return outputPath;
    } else {
        return null;
    }
}

From source file:com.migu.flume.interceptor.plugin.SearchAndAppendInterceptor.java

@Override
public Event intercept(Event event) {
    String origBody = new String(event.getBody(), charset);
    // = Pattern.compile(searchPattern);
    //??/*from w  w w. j ava 2  s  .  co m*/
    String[] searchRegexs = searchPattern.split(",");
    int l = searchRegexs.length;
    StringBuilder appendString = new StringBuilder();
    for (int i = 0; i < l; i++) {
        Pattern pattern = Pattern.compile(searchRegexs[i]);
        Matcher matcher = pattern.matcher(origBody);
        if (matcher.find() && matcher.groupCount() > 0) {
            //?????
            appendString.append(appendDelimiterString + matcher.group(1));
        } else {
            appendString.append(appendDelimiterString + "NULL");
        }
    }

    //Matcher matcher = searchPattern.matcher(origBody);
    // String newBody = matcher.replaceAll(replaceString);
    //
    String newBody = origBody + appendString;
    event.setBody(newBody.getBytes(charset));
    return event;
}