Example usage for java.util.regex Matcher group

List of usage examples for java.util.regex Matcher group

Introduction

In this page you can find the example usage for java.util.regex Matcher group.

Prototype

public String group(String name) 

Source Link

Document

Returns the input subsequence captured by the given named-capturing group during the previous match operation.

Usage

From source file:Main.java

public static long toTime(String expr) {
    Pattern p = Pattern.compile("(-?)([0-9][0-9]):([0-9][0-9]):([0-9][0-9])([\\.:][0-9][0-9]?[0-9]?)?");
    Matcher m = p.matcher(expr);
    if (m.matches()) {
        String minus = m.group(1);
        String hours = m.group(2);
        String minutes = m.group(3);
        String seconds = m.group(4);
        String fraction = m.group(5);
        if (fraction == null) {
            fraction = ".000";
        }/*from w ww  .j a  va2 s . com*/

        fraction = fraction.replace(":", ".");
        long ms = Long.parseLong(hours) * 60 * 60 * 1000;
        ms += Long.parseLong(minutes) * 60 * 1000;
        ms += Long.parseLong(seconds) * 1000;
        if (fraction.contains(":")) {
            ms += Double.parseDouble("0" + fraction.replace(":", ".")) * 40 * 1000; // 40ms == 25fps - simplifying assumption should be ok for here
        } else {
            ms += Double.parseDouble("0" + fraction) * 1000;
        }

        return ms * ("-".equals(minus) ? -1 : 1);
    } else {
        throw new RuntimeException("Cannot match '" + expr + "' to time expression");
    }
}

From source file:Main.java

public static String getToken(String source) {
    String tokenString = "";
    try {/* w  w  w  .  jav  a 2 s  . co  m*/
        String contentString = source;
        String regex = "token=(\\d*)";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(contentString);
        while (matcher.find()) {
            String getToken = matcher.group(1);
            if (getToken != null) {
                tokenString = getToken;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return tokenString;
}

From source file:Main.java

/**
 * Converts percentage string into actual based on a relative number
 *
 * @param percentage percentage string/*  ww w  . ja  v a 2  s  .  c  o  m*/
 * @param relative   relative number
 * @param offset     offset number
 * @return actual float based on relative number
 */

static float fromPercentageToFloat(String percentage, float relative, float offset, float scale) {
    Matcher matched = percentageRegExp.matcher(percentage);
    if (matched.matches()) {
        return Float.valueOf(matched.group(1)) / 100 * relative + offset;
    } else {
        return Float.valueOf(percentage) * scale + offset;
    }
}

From source file:Main.java

/**
 * Get the objid from an URI/Fedora identifier, e.g. from &lt;info:fedora/escidoc:1&gt;<br/> If the provided value
 * does not match the expected pattern, it is returned as provided. Otherwise, the objid is extracted from it and
 * returned.//from  w  w w.j  av  a 2s  .  co  m
 *
 * @param uri The value to get the objid from
 * @return Returns the extracted objid or the provided value.
 */
public static String getIdFromURI(final String uri) {

    if (uri == null) {
        return null;
    }
    final Matcher matcher = PATTERN_GET_ID_FROM_URI_OR_FEDORA_ID.matcher(uri);
    return matcher.find() ? matcher.group(1) : uri;
}

From source file:Main.java

/**
 * Splits the command string around blank spaces avoiding strings surrounded by quotation marks.
 * @param vboxManageCmdLine the command to be handled
 * @return a list of strings of the split command string pieces
 *//*from   w w w  . j av  a2  s  .  c  o m*/
public static List<String> splitCmdLine(String vboxManageCmdLine) {
    List<String> matchList = new ArrayList<String>();
    Pattern regex = Pattern.compile("[^\\s\"']+|\"([^\"]*)\"|'([^']*)'");
    Matcher regexMatcher = regex.matcher(vboxManageCmdLine);
    while (regexMatcher.find()) {
        if (regexMatcher.group(1) != null) {
            // Add double-quoted string without the quotes
            matchList.add(regexMatcher.group(1));
        } else if (regexMatcher.group(2) != null) {
            // Add single-quoted string without the quotes
            matchList.add(regexMatcher.group(2));
        } else {
            // Add unquoted word
            matchList.add(regexMatcher.group());
        }
    }
    return matchList;
}

From source file:Main.java

/**
 * Get parameter value from SDP parameters string with parameter-value
 * format 'key1=value1; ... keyN=valueN'
 * //from  w w  w . jav  a2  s . c  o  m
 * @param paramKey parameter name
 * @param params parameters string
 * @return if parameter exists return {@link String} with value, otherwise
 *         return <code>null</code>
 */
public static String getParameterValue(String paramKey, String params) {
    String value = null;
    if (params != null && params.length() > 0) {
        try {
            java.util.regex.Pattern p = java.util.regex.Pattern.compile("(?<=" + paramKey + "=).*?(?=;|$)");
            java.util.regex.Matcher m = p.matcher(params);
            if (m.find()) {
                value = m.group(0);
            }
        } catch (java.util.regex.PatternSyntaxException ex) {
        }
    }
    return value;
}

From source file:Main.java

/**
 * Match a pattern with a single capturing group and return the content of
 * the capturing group/*from  w ww.  j av a  2 s  .  co  m*/
 *
 * @param text    the text to match against
 * @param pattern the pattern (regular expression) must contain one and only one
 *                capturing group
 * @return
 */
public static String matchPattern(String text, String pattern) {
    // Use regular expression matching to pull the min and max values from
    // the output of gdalinfo
    Pattern p1 = Pattern.compile(pattern, Pattern.MULTILINE);
    Matcher m1 = p1.matcher(text);
    if (m1.find()) {
        if (m1.groupCount() == 1) {
            return m1.group(1);
        } else {
            throw new RuntimeException("error matching pattern " + pattern);
        }
    } else {
        throw new RuntimeException("error matching pattern " + pattern);
    }
}

From source file:Main.java

static String[] getPortValues(String val) {
    Matcher m = pRegEx.matcher(val);
    String[] ret = new String[5];
    if (m.find()) {
        ret[0] = m.group(1);
        ret[1] = m.group(2);/*www . j av  a  2  s.co  m*/
        ret[2] = m.group(3);
        ret[3] = m.group(4);
        ret[4] = m.group(5);

    }
    return ret;
}

From source file:Main.java

/**
 * Check of the balanced tags sup/sub/*from ww  w .ja va2s .  com*/
 * 
 * @param snippet
 * @return <code>true</code> if balanced, <code>false</code> otherwise
 */
public static boolean isBalanced(String snippet) {
    if (snippet == null)
        return true; // ????

    Stack<String> s = new Stack<String>();
    Matcher m = SUBS_OR_SUPS.matcher(snippet);
    while (m.find()) {
        String tag = m.group(1);
        if (tag.toLowerCase().startsWith("su")) {
            s.push(tag);
        } else {
            if (s.empty() || !tag.equals("/" + s.pop())) {
                return false;
            }
        }
    }

    return s.empty();
}

From source file:Main.java

public static String parseOptionalStringAttr(String paramString, Pattern paramPattern) {
    Matcher localMatcher = paramPattern.matcher(paramString);
    if ((localMatcher.find()) && (localMatcher.groupCount() == 1))
        return localMatcher.group(1);
    return null;//  w  w w.j  av a  2  s . co  m
}