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.github.nukesparrow.htmlunit.HUQueryElements.java

public String[] text(String pattern) {
    Pattern p = Pattern.compile(pattern);
    Matcher m = p.matcher(text());
    if (m.find()) {
        String[] r = new String[m.groupCount() + 1];
        for (int i = 0; i <= r.length; i++) {
            r[i] = m.group(i);/*  ww w. ja v a  2  s  . co m*/
        }
        return r;
        //return m.groupCount() >= 1 ? m.group(1) : m.group();
    } else {
        throw new HUQueryException("Pattern " + pattern + " not found in " + text());
    }
}

From source file:fr.cls.atoll.motu.api.rest.MotuRequest.java

/**
 * Search url user pwd.//from w  w w .jav a  2s  .  c om
 * 
 * @param url the url
 * 
 * @return the map< string, string>
 */
public static Map<String, String> searchUrlUserPwd(String url) {
    if (url == null) {
        return null;
    }

    String patternExpression = "(http://)(.*)\\:(.*)\\@(.*)";

    Pattern pattern = Pattern.compile(patternExpression);
    Matcher matcher = pattern.matcher(url);
    // System.out.println(matcher.groupCount());
    if (matcher.groupCount() != 4) {
        return null;
    }
    Map<String, String> map = new HashMap<String, String>();
    if (!(matcher.find())) {
        return null;
    }
    map.put(MotuRequestParametersConstant.PARAM_LOGIN, matcher.group(2));
    map.put(MotuRequestParametersConstant.PARAM_PWD, matcher.group(3));

    StringBuffer stringBuffer = new StringBuffer();
    stringBuffer.append(matcher.group(1));
    stringBuffer.append(matcher.group(4));

    map.put(MotuRequestParametersConstant.PARAM_MODE_URL, stringBuffer.toString());

    return map;
    // while (matcher.find()) {
    // for (int i = 1; i <= matcher.groupCount(); i++) {
    // CharSequence line = matcher.group(i);
    // // System.out.println(line);
    // }
    // }
}

From source file:io.github.hidroh.materialistic.SubmitActivity.java

private void extractUrl(String text) {
    Matcher matcher = Pattern.compile(REGEX_FUZZY_URL).matcher(text);
    if (matcher.find() && matcher.groupCount() >= 3) { // group 1: title, group 2: url, group 3: scheme
        mTitleEditText.setText(trimTitle(matcher.group(1).trim()));
        mContentEditText.setText(matcher.group(2));
    }/*from   w  w w.j  a  v a2  s  .c  o  m*/
}

From source file:com.evrythng.java.wrapper.core.EvrythngApiBuilder.java

public static Builder<AcceptedResourceResponse> postAsynchronously(final String apiKey, final URI uri,
        final Object data, final Pattern extractor) {

    return new CheckedBuilder<AcceptedResourceResponse>(apiKey, HttpMethodBuilder.httpPost(data), uri,
            Status.ACCEPTED, new TypeReference<AcceptedResourceResponse>() {
            }) {/* w w w. j a  v a 2 s  . c o  m*/

        @Override
        public AcceptedResourceResponse execute() throws EvrythngException {
            // Perform request (response status code will be automatically checked):
            HttpResponse response = request();
            String location = null;
            Header header = response.getFirstHeader("location");
            String id = null;
            if (header != null) {
                location = header.getValue();
                if (location != null) {
                    Matcher match = extractor.matcher(location);
                    if (match.matches() && match.groupCount() > 0) {
                        id = match.group(1);
                    }
                }
            }
            return new AcceptedResourceResponse(id, location);
        }
    };
}

From source file:dk.statsbiblioteket.netarchivesuite.arctika.builder.ArcFileNameParserTest.java

private String getMatch(String path) {
    List<Pattern> patterns = Arrays.asList(arc_sb_Pattern, arc_sb_ms_Pattern, arc_kb1_Pattern,
            arc_kb1_ms_Pattern, arc_kb2_Pattern, arc_kb2_ms_Pattern, arc_kb_pligt_Pattern,
            arc_kb_pligt_ms_Pattern, arc_metadata_Pattern, arc_archiveit_Pattern);
    for (Pattern pattern : patterns) {
        Matcher matcher = pattern.matcher(path);
        if (matcher.matches()) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < matcher.groupCount(); i++) {
                if (sb.length() > 0) {
                    sb.append(" ");
                }//w  w w.  j  a  v  a  2 s. c  o m
                sb.append(matcher.group(i));
            }
            return sb.toString();
        }
    }
    return null;
}

From source file:org.apache.eagle.jpm.mr.history.crawler.JHFCrawlerDriverImpl.java

private void readAndCacheLastProcessedDate() throws Exception {
    String lastProcessedDate = JobHistoryZKStateManager.instance().readProcessedDate(partitionId);
    Matcher m = PATTERN_JOB_PROCESS_DATE.matcher(lastProcessedDate);
    if (m.find() && m.groupCount() == 3) {
        this.processDate.year = Integer.parseInt(m.group(1));
        this.processDate.month = Integer.parseInt(m.group(2)) - 1; // zero based month
        this.processDate.day = Integer.parseInt(m.group(3));
    } else {//from  w w w  .  j av  a  2s . c  o m
        throw new IllegalStateException("job lastProcessedDate must have format YYYYMMDD " + lastProcessedDate);
    }

    GregorianCalendar cal = new GregorianCalendar(timeZone);
    cal.set(this.processDate.year, this.processDate.month, this.processDate.day, 0, 0, 0);
    cal.add(Calendar.DATE, 1);
    List<String> list = JobHistoryZKStateManager.instance()
            .readProcessedJobs(String.format(FORMAT_JOB_PROCESS_DATE, cal.get(Calendar.YEAR),
                    cal.get(Calendar.MONTH) + 1, cal.get(Calendar.DAY_OF_MONTH)));
    if (list != null) {
        this.processedJobFileNames = new HashSet<>(list);
    }
}

From source file:com.github.alexfalappa.nbspringboot.cfgprops.highlighting.DataTypeMismatchHighlightingTask.java

@Override
protected void internalRun(CfgPropsParser.CfgPropsParserResult cfgResult, SchedulerEvent se, Document document,
        List<ErrorDescription> errors, Severity severity) {
    logger.fine("Highlighting data type mismatches");
    final Project prj = Utilities.actionsGlobalContext().lookup(Project.class);
    if (prj != null) {
        final SpringBootService sbs = prj.getLookup().lookup(SpringBootService.class);
        final ClassPath cp = getProjectClasspath(prj);
        if (sbs != null && cp != null) {
            final Map<Integer, Pair<String, String>> propLines = cfgResult.getPropLines();
            for (Map.Entry<Integer, Pair<String, String>> entry : propLines.entrySet()) {
                int line = entry.getKey();
                String pName = entry.getValue().first();
                String pValue = entry.getValue().second();
                ConfigurationMetadataProperty cfgMeta = sbs.getPropertyMetadata(pName);
                if (cfgMeta == null) {
                    // try to interpret array notation (strip '[index]' from pName)
                    Matcher mArrNot = pArrayNotation.matcher(pName);
                    if (mArrNot.matches()) {
                        cfgMeta = sbs.getPropertyMetadata(mArrNot.group(1));
                    } else {
                        // try to interpret map notation (see if pName starts with a set of known map props)
                        for (String mapPropertyName : sbs.getMapPropertyNames()) {
                            if (pName.startsWith(mapPropertyName)) {
                                cfgMeta = sbs.getPropertyMetadata(mapPropertyName);
                                break;
                            }/*from www  . j  av  a  2 s .c  o  m*/
                        }
                    }
                }
                if (cfgMeta != null) {
                    final String type = cfgMeta.getType();
                    final ClassLoader cl = cp.getClassLoader(true);
                    if (type.contains("<")) {
                        // maps
                        Matcher mMap = pTwoGenTypeArgs.matcher(type);
                        if (mMap.matches() && mMap.groupCount() == 3) {
                            String keyType = mMap.group(2);
                            check(keyType, pName.substring(pName.lastIndexOf('.') + 1), document, line, errors,
                                    cl, severity);
                            String valueType = mMap.group(3);
                            check(valueType, pValue, document, line, errors, cl, severity);
                        }
                        // collections
                        Matcher mColl = pOneGenTypeArg.matcher(type);
                        if (mColl.matches() && mColl.groupCount() == 2) {
                            String genericType = mColl.group(2);
                            if (pValue.contains(",")) {
                                for (String val : pValue.split("\\s*,\\s*")) {
                                    check(genericType, val, document, line, errors, cl, severity);
                                }
                            } else {
                                check(genericType, pValue, document, line, errors, cl, severity);
                            }
                        }
                    } else {
                        if (pValue.contains(",") && type.endsWith("[]")) {
                            for (String val : pValue.split("\\s*,\\s*")) {
                                check(type.substring(0, type.length() - 2), val, document, line, errors, cl,
                                        severity);
                            }
                        } else {
                            if (type.endsWith("[]")) {
                                check(type.substring(0, type.length() - 2), pValue, document, line, errors, cl,
                                        severity);
                            } else {
                                check(type, pValue, document, line, errors, cl, severity);
                            }
                        }
                    }
                }
            }
        }
    }
    if (!errors.isEmpty()) {
        logger.log(Level.FINE, "Found {0} data type mismatches", errors.size());
    }
}

From source file:com.duroty.controller.actions.GoogieSpellAction.java

protected Vector getMatches(String text) {
    Pattern pattern = Pattern.compile(
            "<c o=\\\"([^\\\"]*)\\\" l=\\\"([^\\\"]*)\\\" s=\\\"([^\\\"]*)\\\">([^<]*)<\\/c>",
            Pattern.CASE_INSENSITIVE);
    Matcher matcher = pattern.matcher(text);

    Vector vector = new Vector();

    while (matcher.find()) {
        int groupCount = matcher.groupCount();
        vector.addElement(matcher.group(0));
        for (int i = 0; i < groupCount; i++) {
            DLog.log(DLog.WARN, this.getClass(), "GROUP: " + i + " >> " + matcher.group(i));
        }//w ww .j a  v a  2  s  . c  o  m
    }

    return vector;
}

From source file:me.StevenLawson.TotalFreedomMod.TFM_Util.java

public static Date parseDateOffset(String time) {
    Pattern timePattern = Pattern.compile("(?:([0-9]+)\\s*y[a-z]*[,\\s]*)?" + "(?:([0-9]+)\\s*mo[a-z]*[,\\s]*)?"
            + "(?:([0-9]+)\\s*w[a-z]*[,\\s]*)?" + "(?:([0-9]+)\\s*d[a-z]*[,\\s]*)?"
            + "(?:([0-9]+)\\s*h[a-z]*[,\\s]*)?" + "(?:([0-9]+)\\s*m[a-z]*[,\\s]*)?"
            + "(?:([0-9]+)\\s*(?:s[a-z]*)?)?", Pattern.CASE_INSENSITIVE);
    Matcher m = timePattern.matcher(time);
    int years = 0;
    int months = 0;
    int weeks = 0;
    int days = 0;
    int hours = 0;
    int minutes = 0;
    int seconds = 0;
    boolean found = false;
    while (m.find()) {
        if (m.group() == null || m.group().isEmpty()) {
            continue;
        }//w  w w  .java2 s  .  c om
        for (int i = 0; i < m.groupCount(); i++) {
            if (m.group(i) != null && !m.group(i).isEmpty()) {
                found = true;
                break;
            }
        }
        if (found) {
            if (m.group(1) != null && !m.group(1).isEmpty()) {
                years = Integer.parseInt(m.group(1));
            }
            if (m.group(2) != null && !m.group(2).isEmpty()) {
                months = Integer.parseInt(m.group(2));
            }
            if (m.group(3) != null && !m.group(3).isEmpty()) {
                weeks = Integer.parseInt(m.group(3));
            }
            if (m.group(4) != null && !m.group(4).isEmpty()) {
                days = Integer.parseInt(m.group(4));
            }
            if (m.group(5) != null && !m.group(5).isEmpty()) {
                hours = Integer.parseInt(m.group(5));
            }
            if (m.group(6) != null && !m.group(6).isEmpty()) {
                minutes = Integer.parseInt(m.group(6));
            }
            if (m.group(7) != null && !m.group(7).isEmpty()) {
                seconds = Integer.parseInt(m.group(7));
            }
            break;
        }
    }
    if (!found) {
        return null;
    }

    Calendar c = new GregorianCalendar();

    if (years > 0) {
        c.add(Calendar.YEAR, years);
    }
    if (months > 0) {
        c.add(Calendar.MONTH, months);
    }
    if (weeks > 0) {
        c.add(Calendar.WEEK_OF_YEAR, weeks);
    }
    if (days > 0) {
        c.add(Calendar.DAY_OF_MONTH, days);
    }
    if (hours > 0) {
        c.add(Calendar.HOUR_OF_DAY, hours);
    }
    if (minutes > 0) {
        c.add(Calendar.MINUTE, minutes);
    }
    if (seconds > 0) {
        c.add(Calendar.SECOND, seconds);
    }

    return c.getTime();
}

From source file:com.formkiq.core.equifax.service.EquifaxFormEventHandler.java

/**
 * Gets 'value' from a string. IE: string instead of [..]
 * IE: Manitoba[MB] returns 'MB'/*from  w w w . ja v a 2 s  .co  m*/
 * IE: Manitoba returns 'Manitoba'
 * @param string String
 * @return String
 */
private String getValue(final String string) {

    if (!StringUtils.isEmpty(string)) {
        Matcher m = VALUE_PATTERN.matcher(string);

        if (m.matches()) {

            if (m.groupCount() == 2) {
                return m.group(2);
            }
        }
    }

    return string;
}