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.hybris.mobile.utility.RegexUtil.java

/**
 * Apply a regex pattern on a value and return the match
 * //from  w ww  . ja  v a  2 s . c  o m
 * @param pattern
 * @param value
 * @return
 */
private static List<String> applyPattern(Pattern pattern, String value, int numberOfElementToMatch) {

    List<String> returnValues = new ArrayList<String>();

    Matcher matcher = pattern.matcher(value);

    if (matcher.matches()) {

        if (matcher.groupCount() == numberOfElementToMatch) {
            for (int i = 1; i <= numberOfElementToMatch; i++) {
                returnValues.add(matcher.group(i));
            }
        }

    }

    if (returnValues.isEmpty()) {
        returnValues.add("");
    }

    return returnValues;

}

From source file:com.hybris.mobile.app.commerce.utils.RegexUtils.java

/**
 * Apply a regex pattern on a value and return the match
 *
 * @param pattern// w  w  w.ja  va2  s  . co  m
 * @param value
 * @return
 */
private static List<String> applyPattern(Pattern pattern, String value, int numberOfElementToMatch) {

    List<String> returnValues = new ArrayList<>();

    Matcher matcher = pattern.matcher(value);

    if (matcher.matches()) {

        if (matcher.groupCount() == numberOfElementToMatch) {
            for (int i = 1; i <= numberOfElementToMatch; i++) {
                returnValues.add(matcher.group(i));
            }
        }

    }

    if (returnValues.isEmpty()) {
        returnValues.add("");
    }

    return returnValues;

}

From source file:com.tek271.reverseProxy.utils.RegexTools.java

/** Find all matches in a string */
public static List<Tuple3<Integer, Integer, String>> findAll(Pattern pattern, String text,
        boolean matchGroups) {
    if (StringUtils.isEmpty(text))
        return Collections.emptyList();

    Matcher matcher = pattern.matcher(text);
    List<Tuple3<Integer, Integer, String>> r = Lists.newArrayList();
    while (matcher.find()) {
        int groupCount = matcher.groupCount();
        if (groupCount == 0 || !matchGroups) {
            r.add(getMatch(matcher, 0));
        } else {/*from   w w  w .ja  va  2 s.  c  o  m*/
            for (int i = 1; i <= groupCount; i++) {
                r.add(getMatch(matcher, i));
            }
        }
    }
    return r;
}

From source file:Main.java

/**
 * Get all the matches for {@code string} compiled by {@code pattern}. If 
 * {@code isGlobal} is true, the return results will only include the 
 * group 0 matches. It is similar to string.match(regexp) in JavaScript.
 * /* www  .  j  av  a  2  s . c  o  m*/
 * @param pattern the regexp
 * @param string the string
 * @param isGlobal similar to JavaScript /g flag
 * 
 * @return all matches
 */
public static String[] match(Pattern pattern, String string, boolean isGlobal) {
    if (pattern == null) {
        throw new NullPointerException("argument 'pattern' cannot be null");
    }
    if (string == null) {
        throw new NullPointerException("argument 'string' cannot be null");
    }

    List<String> matchesList = new ArrayList<String>();

    Matcher matcher = pattern.matcher(string);
    while (matcher.find()) {
        matchesList.add(matcher.group(0));
        if (!isGlobal) {
            for (int i = 1, iEnd = matcher.groupCount(); i <= iEnd; i++) {
                matchesList.add(matcher.group(i));
            }
        }
    }

    return matchesList.toArray(new String[matchesList.size()]);
}

From source file:com.technophobia.substeps.model.Util.java

public static List<Object> getArgs(final String patternString, final String sourceString,
        final Class<?>[] parameterTypes, final Class<? extends Converter<?>>[] converterTypes) {

    log.debug("Util getArgs List<Object> with pattern: " + patternString + " and sourceStr: " + sourceString);

    List<Object> argsList = null;

    final Pattern pattern = Pattern.compile(patternString);
    final Matcher matcher = pattern.matcher(sourceString);

    final int groupCount = matcher.groupCount();

    int argIdx = 0;

    if (matcher.find()) {

        for (int i = 1; i <= groupCount; i++) {
            final String arg = matcher.group(i);

            if (arg != null) {
                if (argsList == null) {
                    argsList = new ArrayList<Object>();
                }/*from ww w  .j  a  v a 2 s .c o m*/

                argsList.add(getObjectArg(arg, parameterTypes[argIdx], converterTypes[argIdx]));

            }
            argIdx++;
        }
    }

    return argsList;

}

From source file:com.fet.crm.smarthome.generic.util.RegularExpressionUtil.java

/**
 * DOCUMENT ME!// w w w .j a  va  2s  .  c  o  m
 * 
 * @param xmlString
 *            DOCUMENT ME!
 * @param tagName
 *            DOCUMENT ME!
 * 
 * @return DOCUMENT ME!
 */
public static String[] getTagValueArray(final String xmlString, final String tagName) {
    final List<String> returnStr = new ArrayList<String>();

    try {
        final Pattern pattern = getPattern(tagName);
        final Matcher matcher = pattern.matcher(xmlString);
        int count;

        while (matcher.find()) {
            count = matcher.groupCount();
            returnStr.add(StringUtils.defaultString(matcher.group(count)));
        }
    } catch (Exception e) {
        LOG.info("Exception occurs in RegularExpressionUtil.getTagValue", e);
    }

    return returnStr.toArray(new String[returnStr.size()]);
}

From source file:com.datumbox.framework.utilities.text.cleaners.HTMLCleaner.java

public static Map<String, List<String>> extractHTMLheaders(String text) {
    Map<String, List<String>> hxtagsMap = new HashMap<>();
    for (Integer i = 1; i <= 6; ++i) {
        hxtagsMap.put("H" + i.toString(), new ArrayList<>());
    }//ww w . ja v a 2s. c o m

    Matcher m = HX_PATTERN.matcher(text);
    while (m.find()) {
        if (m.groupCount() == 2) {
            String tagType = m.group(1).toUpperCase();
            String content = m.group(2);
            hxtagsMap.get(tagType).add(clear(content));
        }
    }
    return hxtagsMap;
}

From source file:com.datumbox.framework.utilities.text.cleaners.HTMLCleaner.java

public static Map<String, String> extractMetatags(String text) {
    Map<String, String> metatagsMap = new HashMap<>();

    Matcher m = METATAG_PATTERN.matcher(text);
    while (m.find()) {
        if (m.groupCount() == 2) {
            String name = m.group(1);
            String content = m.group(2);
            metatagsMap.put(clear(name), clear(content));
        }/*from ww w  . j a v a 2s.c o  m*/
    }
    return metatagsMap;
}

From source file:com.datumbox.framework.utilities.text.cleaners.HTMLCleaner.java

public static Map<HyperlinkPart, List<String>> extractHyperlinks(String text) {
    Map<HyperlinkPart, List<String>> hyperlinksMap = new HashMap<>();
    hyperlinksMap.put(HyperlinkPart.HTMLTAG, new ArrayList<>());
    hyperlinksMap.put(HyperlinkPart.URL, new ArrayList<>());
    hyperlinksMap.put(HyperlinkPart.ANCHORTEXT, new ArrayList<>());

    Matcher m = HYPERLINK_PATTERN.matcher(text);
    while (m.find()) {
        if (m.groupCount() == 2) {
            String tag = m.group(0);
            String url = m.group(1);
            String anchortext = m.group(2);
            hyperlinksMap.get(HyperlinkPart.HTMLTAG).add(tag);
            hyperlinksMap.get(HyperlinkPart.URL).add(url);
            hyperlinksMap.get(HyperlinkPart.ANCHORTEXT).add(anchortext);
        }//w ww . j  a  v a 2s. c  om
    }
    return hyperlinksMap;
}

From source file:com.aliyun.openservices.odps.console.commands.ShowTablesCommand.java

static String getProjectName(Matcher m) {
    String projectName = null;//www . ja v  a  2s. co m

    if (m.matches() && m.groupCount() == 3) {
        projectName = m.group(3);
    }

    return projectName;
}