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:Main.java

private static HashMap<String, String> parseContent(String src) {
    HashMap<String, String> map = new HashMap<String, String>();
    String patternStr = "http://(.*)\\?chat_id=[-]?(\\d+)&chat_name=(.+)";

    Pattern p = Pattern.compile(patternStr);
    Matcher m = p.matcher(src);
    if (!m.find()) {
        return null;
    } else if (m.groupCount() == 3) {
        map.put("chat_id", m.group(2));
        map.put("chat_name", m.group(3));
        return map;
    }//from ww  w  .j a  va2  s . c  om

    return null;
}

From source file:de.pixida.logtest.logreaders.PatternMatchingsStripper.java

public static String strip(final Matcher matcher, final String value) {
    if (matcher.groupCount() == 0) {
        return value;
    }/*  w ww. java2  s .  c  o m*/

    // Remove optional matches which were empty and filter nested matches
    final List<Pair<Integer, Integer>> realMatches = new ArrayList<>();
    int lastMostOuterMatchEnd = -1;
    for (int i = 1; i <= matcher.groupCount(); i++) {
        if (matcher.start(i) != -1) {
            if (matcher.end(i) <= lastMostOuterMatchEnd) {
                continue;
            }
            lastMostOuterMatchEnd = matcher.end(i);
            realMatches.add(Pair.of(matcher.start(i), matcher.end(i)));
        }
    }
    if (realMatches.isEmpty()) {
        return value;
    }

    // Removal
    final StringBuilder sb = new StringBuilder(value.substring(0, realMatches.get(0).getLeft()));
    for (int i = 0; i < realMatches.size(); i++) {
        // Assumption: Substring before start of match was already appended
        if (i + 1 < realMatches.size()) {
            sb.append(value.substring(realMatches.get(i).getRight(), realMatches.get(i + 1).getLeft()));
        } else {
            sb.append(value.substring(realMatches.get(i).getRight()));
        }
    }
    return sb.toString();
}

From source file:com.qumoon.commons.TaobaoUtils.java

/**
 * ?? url ? num iid/*from w w  w  .  j  a va 2 s . c  o m*/
 */
public static Long getItemUrlNumIid(String url) {
    Matcher matcher = ITEM_NUM_IID_PATTERN.matcher(url);
    if (matcher.find() && matcher.groupCount() > 1) {
        String numIidStr = matcher.group(2);
        if (NumberUtils.isNumber(numIidStr)) {
            return Long.valueOf(numIidStr);
        } else {
            return null;
        }
    }
    return null;
}

From source file:com.art4ul.jcoon.util.HttpRequestUtil.java

public static void addKeyValueParams(Context context, String[] stringArray, AddingStatagy addingStatagy) {
    if (stringArray != null) {
        Pattern pattern = Pattern.compile(KEY_VALUE_REGEX_PATTERN);
        for (String str : stringArray) {
            Matcher matcher = pattern.matcher(str);
            while (matcher.find()) {
                if (matcher.groupCount() == 2) {
                    String headerName = matcher.group(1);
                    String headerValue = matcher.group(2);
                    addingStatagy.add(headerName.trim(), headerValue.trim());
                    //context.getHttpHeaders().add(headerName.trim(), headerValue.trim());
                }/*w  ww  .  j  a v  a 2 s. c  om*/
            }
        }
    }
}

From source file:Utils.java

public static List<String> getFound(String contents, String regex) {
    if (isEmpty(regex) || isEmpty(contents)) {
        return null;
    }//from w  w  w. java 2 s.c o m
    List<String> results = new ArrayList<String>();
    Pattern pattern = Pattern.compile(regex, Pattern.UNICODE_CASE);
    Matcher matcher = pattern.matcher(contents);

    while (matcher.find()) {
        if (matcher.groupCount() > 0) {
            results.add(matcher.group(1));
        } else {
            results.add(matcher.group());
        }
    }
    return results;
}

From source file:edu.cmu.sv.modelinference.common.formats.st.STConfig.java

public static GridPartitions extractGridPartitions(String optionString) throws ParseException {
    GridPartitions parts = new GridPartitions();
    parts.horiz = STModelInferer.DEF_PARTITIONS;
    parts.vert = STModelInferer.DEF_PARTITIONS;
    String optionStr = optionString.trim();
    String regex = "([0-9]+)x?([0-9]+)?";
    Matcher dimPatMatcher = Pattern.compile(regex).matcher(optionStr);
    if (dimPatMatcher.find()) {
        if (dimPatMatcher.groupCount() == 1) {
            parts.horiz = parts.vert = Integer.parseInt(dimPatMatcher.group(1));
        } else if (dimPatMatcher.groupCount() == 2) {
            String horizStr = dimPatMatcher.group(1);
            parts.horiz = Integer.parseInt(horizStr);
            String vertStr = dimPatMatcher.group(2);
            if (vertStr != null) //weird that this check is needed...
                parts.vert = Integer.parseInt(vertStr);
        } else {//from  w  w w  .j  ava 2  s.  co  m
            throw new ParseException("Dimensions must adhere to regex " + regex);
        }
    } else {
        throw new ParseException("Dimensions must adhere to regex " + regex);
    }
    return parts;
}

From source file:emily.util.YTUtil.java

/**
 * Extracts the playlistcode from a yt url
 *
 * @param url the url/*from  w  w  w. ja v a  2s.  co  m*/
 * @return playlistcode || null if not found
 */
public static String getPlayListCode(String url) {
    Matcher matcher = yturl.matcher(url);
    if (matcher.find()) {
        if (matcher.groupCount() == 2) {
            return matcher.group(2);
        }
    }
    return null;
}

From source file:eu.eubrazilcc.lvl.storage.mongodb.MongoDBComparison.java

public static final BasicDBObject mongoNumeriComparison(final String field, final String expression) {
    checkArgument(isNotBlank(field), "Uninitialized or invalid field");
    checkArgument(isNotBlank(expression), "Uninitialized or invalid expression");
    final Matcher matcher = LOGICAL_PATTERN.matcher(expression.trim());
    checkState(matcher.find() && matcher.groupCount() == 3, "Unsupported expression: " + expression);
    String operator = null;// www  .java 2 s  . com
    Integer quantity = null;
    for (int i = 1; i < 4; i++) {
        switch (i) {
        case 0: // skip first position where the input is stored
            break;
        case 1: // grab operator
            operator = parseLogicalOperator(matcher.group(i));
            break;
        case 2: // skip empty spaces            
            break;
        case 3: // grab quantity
            quantity = Integer.valueOf(matcher.group(i));
            break;
        default:
            throw new IllegalStateException("Unsupported expression: " + expression);
        }
    }
    return isBlank(operator) ? new BasicDBObject(field, quantity)
            : new BasicDBObject(field, new BasicDBObject(operator, quantity));
}

From source file:io.github.seiferma.jameica.hibiscus.dkb.creditcard.synchronize.scraper.DKBTransactionScraper.java

private static boolean matches(String ccNumber, String anonymizedCcNumber) {
    String candidate = StringUtils.trim(anonymizedCcNumber);
    Pattern p = Pattern.compile(".*?([0-9]{4})[0-9*]*([0-9]{4}).*");
    Matcher m = p.matcher(candidate);
    if (!m.matches() || m.groupCount() < 2) {
        return false;
    }//from ww w . j a va 2 s .c o  m
    String foundFirstDigits = m.group(1);
    String foundLastDigits = m.group(2);

    String requiredFirstDigits = ccNumber.substring(0, 4);
    String requiredLastDigits = ccNumber.substring(ccNumber.length() - 4, ccNumber.length());

    return requiredFirstDigits.equals(foundFirstDigits) && requiredLastDigits.equals(foundLastDigits);
}

From source file:ar.com.zauber.commons.spring.web.CommandURLParameterGenerator.java

/**
 * @see #getURLParameter(Class, Set)// w  w w . jav  a 2s .  c o  m
 */
public static String getURLParameter(final Class<?> cmdClass, final Map<Class<?>, Object> values) {
    final StringBuilder sb = new StringBuilder("?");
    final Pattern getter = Pattern.compile("^get(.*)$");
    boolean first = true;

    // look for getters
    for (final Method method : cmdClass.getMethods()) {
        final Matcher matcher = getter.matcher(method.getName());
        if (matcher.lookingAt() && matcher.groupCount() == 1 && method.getParameterTypes().length == 0
                && values.containsKey(method.getReturnType())) {
            try {
                cmdClass.getMethod("set" + matcher.group(1), new Class[] { method.getReturnType() });
                if (!first) {
                    sb.append("&");
                } else {
                    first = false;
                }

                sb.append(URLEncoder.encode(matcher.group(1).toLowerCase(), CHARSET));
                sb.append("=");
                sb.append(URLEncoder.encode(values.get(method.getReturnType()).toString(), CHARSET));
            } catch (Exception e) {
                // skip
            }
        }
    }

    return sb.toString();
}