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:io.amient.examples.wikipedia.WikipediaMessage.java

private static WikipediaMessage parseText(String raw) {
    Pattern p = Pattern.compile("\\[\\[(.*)\\]\\]\\s(.*)\\s(.*)\\s\\*\\s(.*)\\s\\*\\s\\(\\+?(.\\d*)\\)\\s(.*)");
    Matcher m = p.matcher(raw);

    if (!m.find()) {
        throw new IllegalArgumentException("Could not parse message: " + raw);
    } else if (m.groupCount() != 6) {
        throw new IllegalArgumentException("Unexpected parser group count: " + m.groupCount());
    } else {/*from w  w w .  j av  a2  s . c om*/
        WikipediaMessage result = new WikipediaMessage();

        result.title = m.group(1);
        String flags = m.group(2);
        result.diffUrl = m.group(3);
        result.user = m.group(4);
        result.byteDiff = Integer.parseInt(m.group(5));
        result.summary = m.group(6);

        result.isNew = flags.contains("N");
        result.isMinor = flags.contains("M");
        result.isUnpatrolled = flags.contains("!");
        result.isBotEdit = flags.contains("B");

        result.type = result.title.startsWith("Special:") ? Type.SPECIAL
                : (result.title.startsWith("Talk:") ? Type.TALK : Type.EDIT);
        return result;
    }
}

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

public static String[] getArgs(final String patternString, final String sourceString,
        final String[] keywordPrecedence) {

    log.debug("Util getArgs String[] with pattern: " + patternString + " and sourceStr: " + sourceString);

    String[] rtn = null;//  ww w  .j a  va  2  s  .  c  om

    ArrayList<String> argsList = null;

    String patternCopy = new String(patternString);
    if (keywordPrecedence != null && StringUtils.startsWithAny(patternString, keywordPrecedence)) {
        //
        for (String s : keywordPrecedence) {

            patternCopy = StringUtils.removeStart(patternCopy, s);
        }

        patternCopy = "(?:" + StringUtils.join(keywordPrecedence, "|") + ")" + patternCopy;
    }

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

    final int groupCount = matcher.groupCount();

    // TODO - this doesn't work if we're not doing strict matching
    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<String>();
                }
                argsList.add(arg);
            }
        }
    }

    if (argsList != null) {
        rtn = argsList.toArray(new String[argsList.size()]);

        if (log.isDebugEnabled()) {

            final StringBuilder buf = new StringBuilder();
            buf.append("returning args: ");

            for (final String s : argsList) {

                buf.append("[").append(s).append("] ");
            }

            log.debug(buf.toString());
        }

    }

    return rtn;
}

From source file:org.apache.struts.maven.snippetextractor.parser.SnippetParser.java

private static boolean isSnippetStart(String string, Matcher matcher) {
    if (matcher == null) {
        matcher = SNIPPET_START_PATTERN.matcher(string);
    }/*from   ww w  . j  a v  a  2 s. c  o m*/
    return matcher.find() && matcher.groupCount() == 3;
}

From source file:com.heliosapm.opentsdb.client.opentsdb.OpenTsdbPutResponseHandler.java

/**
 * Extracts the failure and success counts from the passed matcher
 * @param m The matcher containing the counts
 * @return the counts/*from   ww  w.ja v  a 2s.co  m*/
 */
public static int[] getCountsFromMatcher(final Matcher m) {
    final int grpCount = m.groupCount();
    final int[] counts = new int[2];
    try {
        counts[0] = Integer.parseInt(m.group(grpCount == 3 ? 2 : 1));
        counts[1] = Integer.parseInt(m.group(grpCount == 3 ? 3 : 2));
    } catch (Exception x) {
        return null;
    }
    return counts;
}

From source file:de.tudarmstadt.ukp.csniper.webapp.search.cqp.CqpEngine.java

public static List<CqpMacro> getMacros() {
    List<CqpMacro> macros = new ArrayList<CqpMacro>();

    boolean open = false;
    CqpMacro currentMacro = null;//from w  w w  . j  a  va  2s  .c  om
    String lastComment = "";

    InputStream is = null;
    try {
        is = ResourceUtils.resolveLocation(macrosLocation, null, null).openStream();
        for (LineIterator li = IOUtils.lineIterator(is, "UTF-8"); li.hasNext();) {

            String line = li.next();
            String n = line.toLowerCase().trim();

            // comment
            if (n.startsWith("#") && !open) {
                lastComment = line;
                continue;
            }

            if (n.startsWith("macro") && !open) {
                currentMacro = new CqpMacro();
                Pattern p = Pattern.compile("MACRO\\s+(\\w+)\\s*\\((\\d+)\\)");
                Matcher m = p.matcher(line.trim());
                if (m.matches() && m.groupCount() >= 2) {
                    currentMacro.setName(m.group(1));
                    currentMacro.setParamCount(Integer.parseInt(m.group(2)));
                    currentMacro.setComment(lastComment);
                    currentMacro.setBody(new ArrayList<String>());
                } else {
                    // throw new
                }
                continue;
            }

            if (n.startsWith("(") && !open) {
                open = true;
                continue;
            }

            if (n.startsWith(")") && open) {
                if (n.startsWith(");") || (li.hasNext() && li.next().trim().startsWith(";"))) {
                    open = false;
                    macros.add(currentMacro);
                    continue;
                }
            }

            if (open) {
                currentMacro.getBody().add(line.trim());
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        closeQuietly(is);
    }

    return macros;
}

From source file:io.github.seiferma.jameica.hibiscus.dkb.creditcard.synchronize.csvparser.DKBCsvParser.java

private static DKBTransaction createTransaction(CSVRecord record) throws IOException {
    String bookedString = record.get(0);
    boolean isBooked = "Ja".equals(bookedString);

    String valueDateString = record.get(1);
    Date valueDate;//from  w  ww.ja v a 2s.  com
    try {
        valueDate = DateUtils.parseDate(valueDateString);
    } catch (ParseException e) {
        throw new IOException("Could not parse value date.", e);
    }

    String bookingDateString = record.get(2);
    Date bookingDate;
    try {
        bookingDate = DateUtils.parseDate(bookingDateString);
    } catch (ParseException e) {
        throw new IOException("Could not parse booking date.", e);
    }

    String description = record.get(3);
    description = description.replaceAll("    ", "");

    String amountString = record.get(4);
    amountString = amountString.replace(".", "");
    Pattern amountPattern = Pattern.compile("(-?[0-9]+),([0-9]{1,2})");
    Matcher amountMatcher = amountPattern.matcher(amountString);
    if (!amountMatcher.matches() || amountMatcher.groupCount() != 2) {
        throw new IOException(String.format("Could not parse transaction amount (%s).", amountString));
    }

    int amountInCents = getCentsFromString(amountMatcher.group(1), amountMatcher.group(2));

    return DKBTransactionImpl.create().setIsBooked(isBooked).setValueDate(valueDate).setBookingDate(bookingDate)
            .setDescription(description).setAmountInCents(amountInCents).build();

}

From source file:org.codehaus.griffon.commons.GriffonResourceUtils.java

/**
 * Get the path relative to an artefact folder under griffon-app i.e:
 *
 * Input: /usr/joe/project/griffon-app/conf/BootStrap.groovy
 * Output: BootStrap.groovy/*from  w  w w. ja  v a2  s. co m*/
 *
 * Input: /usr/joe/project/griffon-app/domain/com/mystartup/Book.groovy
 * Output: com/mystartup/Book.groovy
 *
 * @param path The path to evaluate
 * @return The path relative to the root folder griffon-app
 */
public static String getPathFromRoot(String path) {
    for (Pattern pattern : COMPILER_ROOT_PATTERNS) {
        Matcher m = pattern.matcher(path);
        if (m.find()) {
            return m.group(m.groupCount());
        }
    }
    return null;
}

From source file:com.atypon.wayf.database.QueryMapper.java

private static List<String> parseQuery(String query) {
    List<String> parsedQuery = parsedQueryCache.get(query);

    if (parsedQuery == null) {
        Matcher result = PATTERN.matcher(query);

        parsedQuery = new LinkedList<>();

        while (result.find()) {
            for (int i = 1; i <= result.groupCount(); i++) {
                String field = result.group(i);

                if (!FIELD_BLACKLIST.contains(field)) {
                    parsedQuery.add(field);
                }// w w w  . j  av  a 2s.c o  m
            }
        }

        parsedQueryCache.put(query, parsedQuery);
    }

    return parsedQuery;
}

From source file:com.alibaba.otter.common.push.datasource.media.MediaPushDataSourceHandler.java

public static MediaInfo parseMediaInfo(String url) {
    if (StringUtils.isEmpty(url)) {
        return null;
    }// w  ww  . j ava 2s . c o m
    Matcher matcher = PATTERN.matcher(url.trim());
    if (!matcher.matches()) {
        return null;
    }

    if (matcher.groupCount() < 1) {
        throw new IllegalArgumentException(
                url + " is a media push datasource but have no enough info for groupKey.");
    }
    return new MediaInfo(matcher.group(1));
}

From source file:com.linkedin.databus2.producers.gg.GGEventGenerationFactory.java

public static String uriToGGDir(String uri) throws DatabusException {
    if (uri == null) {
        throw new DatabusException("uri passed is null and not valid");
    }//from w  w w  .ja  v  a 2  s.  c o m

    Pattern pattern = Pattern.compile("gg://(.*):(.*)");
    Matcher matcher = pattern.matcher(uri);
    if (!matcher.matches() || matcher.groupCount() != 2) {
        throw new DatabusException("Expected uri format for gg path not found");
    }

    return matcher.group(1);
}