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.appcelerator.titanium.util.TiUIHelper.java

public static int getSizeUnits(final String size) {
    int units = TypedValue.COMPLEX_UNIT_PX;
    String unitString = null;//w ww  .java  2  s .  co  m

    if (size != null) {
        Matcher m = SIZED_VALUE.matcher(size.trim());
        if (m.matches()) {
            if (m.groupCount() == 2) {
                unitString = m.group(2);
            }
        }
    }

    if (unitString == null) {
        unitString = TiApplication.getInstance().getDefaultUnit();
    }

    if (TiDimension.UNIT_PX.equals(unitString) || TiDimension.UNIT_SYSTEM.equals(unitString)) {
        units = TypedValue.COMPLEX_UNIT_PX;
    } else if (TiDimension.UNIT_PT.equals(unitString)) {
        units = TypedValue.COMPLEX_UNIT_PT;
    } else if (TiDimension.UNIT_DP.equals(unitString) || TiDimension.UNIT_DIP.equals(unitString)) {
        units = TypedValue.COMPLEX_UNIT_DIP;
    } else if (TiDimension.UNIT_SP.equals(unitString) || TiDimension.UNIT_SIP.equals(unitString)) {
        units = TypedValue.COMPLEX_UNIT_SP;
    } else if (TiDimension.UNIT_MM.equals(unitString)) {
        units = TypedValue.COMPLEX_UNIT_MM;
    } else if (TiDimension.UNIT_CM.equals(unitString)) {
        units = TiDimension.COMPLEX_UNIT_CM;
    } else if (TiDimension.UNIT_IN.equals(unitString)) {
        units = TypedValue.COMPLEX_UNIT_IN;
    } else {
        if (unitString != null) {
            Log.w(TAG, "Unknown unit: " + unitString, Log.DEBUG_MODE);
        }
    }

    return units;
}

From source file:cn.edu.hfut.dmic.contentextractor.ContentExtractor.java

protected String getTime(Element contentElement, String regex) throws Exception {
    Pattern pattern = Pattern.compile(regex);
    Element current = contentElement;
    for (int i = 0; i < 2; i++) {
        if (current != null && current != doc.body()) {
            Element parent = current.parent();
            if (parent != null) {
                current = parent;/*w ww . java 2s. c o  m*/
            }
        }
    }
    for (int i = 0; i < 6; i++) {
        if (current == null) {
            break;
        }
        String currentHtml = current.outerHtml();
        Matcher matcher = pattern.matcher(currentHtml);
        if (matcher.find()) {
            srcTime = matcher.group(0);
            StringBuilder sb = new StringBuilder(
                    matcher.group(1) + "-" + format(matcher.group(2)) + "-" + format(matcher.group(3)));
            if (matcher.groupCount() >= 4) {
                sb.append(" ").append(format(matcher.group(4)));
            }
            if (matcher.groupCount() >= 5) {
                sb.append(":").append(format(matcher.group(5)));
            }
            if (matcher.groupCount() >= 6) {
                sb.append(":").append(format(matcher.group(6)));
            }
            return sb.toString();
        }
        if (current != doc.body()) {
            current = current.parent();
        }
    }
    return "";
}

From source file:com.mousebird.maply.MaplyStarModel.java

public MaplyStarModel(String fileName, String imageName, Activity activity) throws IOException {
    AssetManager assetMgr = activity.getAssets();
    InputStream inputStream = null;
    String[] paths = assetMgr.list("maplystarmodel");
    for (String path : paths) {
        if (path.equals(imageName)) {
            //image
            BufferedInputStream bufferedInputStream = null;
            try {
                inputStream = assetMgr.open("maplystarmodel/" + path);
                bufferedInputStream = new BufferedInputStream(inputStream);
                image = BitmapFactory.decodeStream(bufferedInputStream);
            } finally {
                if (bufferedInputStream != null) {
                    try {
                        bufferedInputStream.close();
                    } catch (IOException e) {
                    }/*from  ww  w  .  ja v a 2  s. c o m*/
                }
            }
        }
        if (path.equals(fileName)) {
            //data
            Matcher m;
            try {
                inputStream = assetMgr.open("maplystarmodel/" + path);
                String stars = IOUtils.toString(inputStream, Charset.defaultCharset());
                Pattern p = Pattern.compile("[-]?[0-9]*\\.?[0-9]+");
                m = p.matcher(stars);
            } finally {
                if (inputStream != null) {
                    try {
                        inputStream.close();
                    } catch (IOException e) {
                    }
                }
            }

            this.stars = new ArrayList<SingleStar>();
            if (m.groupCount() % 3 == 0) {
                int i = 0;
                SingleStar s = null;
                while (m.find()) {
                    switch (i) {
                    case 0:
                        s = new SingleStar();
                        s.ra = Float.valueOf(m.group());
                        i++;
                        break;
                    case 1:
                        s.dec = Float.valueOf(m.group());
                        i++;
                        break;
                    case 2:
                        s.mag = Float.valueOf(m.group());
                        this.stars.add(s);
                        i = 0;
                        break;
                    default:
                        break;
                    }
                }
            }
        }
    }
}

From source file:org.appcelerator.titanium.util.TiUIHelper.java

public static void getSizeAndUnits(final String size, final float[] result) {
    int units = TypedValue.COMPLEX_UNIT_PX;
    float value = 15.0f;
    String unitString = null;// w w w .ja  v a 2 s .  c  o m

    if (size != null) {
        Matcher m = SIZED_VALUE.matcher(size.trim());
        if (m.matches()) {
            if (m.groupCount() == 2) {
                unitString = m.group(2);
                value = Float.parseFloat(m.group(1));
            }
        }
    }

    if (unitString == null) {
        unitString = TiApplication.getInstance().getDefaultUnit();
    }

    if (TiDimension.UNIT_PX.equals(unitString) || TiDimension.UNIT_SYSTEM.equals(unitString)) {
        units = TypedValue.COMPLEX_UNIT_PX;
    } else if (TiDimension.UNIT_PT.equals(unitString)) {
        units = TypedValue.COMPLEX_UNIT_PT;
    } else if (TiDimension.UNIT_DP.equals(unitString) || TiDimension.UNIT_DIP.equals(unitString)) {
        units = TypedValue.COMPLEX_UNIT_DIP;
    } else if (TiDimension.UNIT_SP.equals(unitString) || TiDimension.UNIT_SIP.equals(unitString)) {
        units = TypedValue.COMPLEX_UNIT_SP;
    } else if (TiDimension.UNIT_MM.equals(unitString)) {
        units = TypedValue.COMPLEX_UNIT_MM;
    } else if (TiDimension.UNIT_CM.equals(unitString)) {
        units = TypedValue.COMPLEX_UNIT_MM;
        value *= 10;
    } else if (TiDimension.UNIT_IN.equals(unitString)) {
        units = TypedValue.COMPLEX_UNIT_IN;
    } else {
        if (unitString != null) {
            Log.w(TAG, "Unknown unit: " + unitString, Log.DEBUG_MODE);
        }
    }
    result[0] = units;
    result[1] = value;
}

From source file:edu.isi.wings.portal.classes.config.Config.java

private long getSizeFromString(String sizeString) {
    long kb = 1024;
    long mb = kb * kb;
    long gb = kb * mb;
    long tb = kb * gb;

    Pattern pat = Pattern.compile("(\\d+)\\s*([KkMmGgTt])[Bb]?");
    Matcher mat = pat.matcher(sizeString);
    if (mat.find()) {
        long size = Long.parseLong(mat.group(1));
        if (mat.groupCount() > 1) {
            String units = mat.group(2).toLowerCase();
            if (units.equals("k"))
                return size * kb;
            if (units.equals("m"))
                return size * mb;
            if (units.equals("g"))
                return size * gb;
            if (units.equals("t"))
                return size * tb;
        }//from  w w  w .  ja  va  2 s  .co  m
        return size;
    }
    return 0;
}

From source file:org.betaconceptframework.astroboa.test.model.query.parser.CriterionParserTest.java

/**
 * If provided value is a valid ISO8601 date then equivalent calendar is returned
 * /*  w w w .  j  a  v  a2s .  co  m*/
 * @param value
 * @return
 * @throws ParseException 
 */
private Calendar checkIfValueIsISO8601Date(String value) {

    if (StringUtils.isBlank(value)) {
        return null;
    }

    Calendar date = null;

    try {
        Matcher dateTimeMatcher = ISO8601Pattern.matcher(value);

        if (dateTimeMatcher.matches()) {

            StringBuilder pattern = new StringBuilder("yyyy-MM-dd'T'HH:mm:ss");
            String timeZoneId = null;
            //We must decide which pattern to use
            //At this point this is the minimum

            //Group 7 corresponds to milli seconds
            if (dateTimeMatcher.groupCount() >= 7 && dateTimeMatcher.group(7) != null) {
                pattern.append(".SSS");
            }
            if (dateTimeMatcher.groupCount() >= 8 && dateTimeMatcher.group(8) != null) {
                if (!"Z".equals(dateTimeMatcher.group(8))) {
                    //Keep UTC info to look for time zone when Calendar object will be created, 
                    //as SimpleDateformat which is used in DateUtils
                    //cannot handle time zone designator
                    timeZoneId = "GMT" + dateTimeMatcher.group(8);
                    value = value.replace(dateTimeMatcher.group(8), "");
                } else {
                    timeZoneId = "GMT";
                    value = value.replace("Z", "");
                }
            }

            date = (Calendar) DateUtils.fromString(value, pattern.toString());

            if (timeZoneId != null) {
                //Now that date is found we should define its TimeZone
                TimeZone timeZone = TimeZone.getTimeZone(timeZoneId);
                if (!timeZone.getID().equals(timeZoneId)) {
                    // Time Zone is not valid
                    throw new CmsException("Invalid time zone in date value " + value);
                }

                date.setTimeZone(timeZone);
            }
        } else {
            //check for simple date
            Matcher dateMatcher = ISO8601DatePattern.matcher(value);

            if (dateMatcher.matches()) {
                date = (Calendar) DateUtils.fromString(value, ISO8601_DATE_FORMAT);
            }
        }

    } catch (Exception e) {
        //Probably not a date value. Ignore exception
        return null;
    }

    return date;

}

From source file:gtu._work.ui.RegexDirReplacer.java

/**
 * @param fromPattern//from   w w  w.  j  a  va2s. c  o  m
 *            ???pattern
 * @param toFormat
 *            ??pattern
 * @param replaceText
 *            ??
 */
String replacer(String fromPattern, String toFormat, String replaceText, File file) {
    String errorRtn = replaceText.toString();
    try {
        Pattern pattern = Pattern.compile(fromPattern);
        Matcher matcher = pattern.matcher(replaceText);

        StringBuilder sb = new StringBuilder();
        int startPos = 0;

        String tempStr = null;
        for (; matcher.find();) {
            tempStr = toFormat.toString();
            sb.append(replaceText.substring(startPos, matcher.start()));
            for (int ii = 0; ii <= matcher.groupCount(); ii++) {
                tempStr = tempStr.replaceAll("#" + ii + "#", Matcher.quoteReplacement(matcher.group(ii)));
                System.out.println("group[" + ii + "] -- " + matcher.group(ii) + "\t--> " + tempStr);
            }
            sb.append(tempStr);
            startPos = matcher.end();
        }

        sb.append(replaceText.substring(startPos));

        return sb.toString();
    } catch (Exception ex) {
        // JOptionPaneUtil.newInstance().iconErrorMessage().showMessageDialog(ex.getMessage(),
        // getTitle());
        errMsg.append(file.getName() + ":" + ex + "\n");
        return errorRtn;
    }
}

From source file:org.apache.hadoop.hbase.MetaTableAccessor.java

/**
 * Parses the replicaId from the server column qualifier. See top of the class javadoc
 * for the actual meta layout//from  w w w  .  j ava 2s .com
 * @param serverColumn the column qualifier
 * @return an int for the replicaId
 */
@VisibleForTesting
static int parseReplicaIdFromServerColumn(byte[] serverColumn) {
    String serverStr = Bytes.toString(serverColumn);

    Matcher matcher = SERVER_COLUMN_PATTERN.matcher(serverStr);
    if (matcher.matches() && matcher.groupCount() > 0) {
        String group = matcher.group(1);
        if (group != null && group.length() > 0) {
            return Integer.parseInt(group.substring(1), 16);
        } else {
            return 0;
        }
    }
    return -1;
}

From source file:org.apache.maven.doxia.parser.AbstractXmlParser.java

/**
 * Handle entities defined in external doctypes as the following:
 * <pre>//  w  w  w  .jav  a 2s.c  o m
 * &lt;!DOCTYPE foo [
 *   &lt;!-- These are the entity sets for ISO Latin 1 characters for the XHTML --&gt;
 *   &lt;!ENTITY % HTMLlat1 PUBLIC "-//W3C//ENTITIES Latin 1 for XHTML//EN"
 *          "http://www.w3.org/TR/xhtml1/DTD/xhtml-lat1.ent"&gt;
 *   %HTMLlat1;
 * ]&gt;
 * </pre>
 *
 * @param parser not null
 * @param text not null
 * @throws XmlPullParserException if any
 */
private void addDTDEntities(XmlPullParser parser, String text) throws XmlPullParserException {
    int entitiesCount = StringUtils.countMatches(text, ENTITY_START);
    if (entitiesCount > 0) {
        final String txt = StringUtils.replace(text, ENTITY_START, "\n" + ENTITY_START);
        BufferedReader reader = new BufferedReader(new StringReader(txt));
        String line;
        String tmpLine = "";
        try {
            Matcher matcher;
            while ((line = reader.readLine()) != null) {
                tmpLine += "\n" + line;
                matcher = PATTERN_ENTITY_1.matcher(tmpLine);
                if (matcher.find() && matcher.groupCount() == 7) {
                    String entityName = matcher.group(2);
                    String entityValue = matcher.group(5);

                    addEntity(parser, entityName, entityValue);
                    tmpLine = "";
                } else {
                    matcher = PATTERN_ENTITY_2.matcher(tmpLine);
                    if (matcher.find() && matcher.groupCount() == 8) {
                        String entityName = matcher.group(2);
                        String entityValue = matcher.group(5);

                        addEntity(parser, entityName, entityValue);
                        tmpLine = "";
                    }
                }
            }
        } catch (IOException e) {
            // nop
        } finally {
            IOUtil.close(reader);
        }
    }
}

From source file:org.eclipse.rdf4j.http.client.RDF4JProtocolSession.java

public void setRepository(String repositoryURL) {
    // Try to parse the server URL from the repository URL
    Pattern urlPattern = Pattern.compile("(.*)/" + Protocol.REPOSITORIES + "/[^/]*/?");
    Matcher matcher = urlPattern.matcher(repositoryURL);

    if (matcher.matches() && matcher.groupCount() == 1) {
        setServerURL(matcher.group(1));/*from  w  w w  . j  a  va 2s. co m*/
    }

    setQueryURL(repositoryURL);
}