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:com.haulmont.cuba.gui.components.SizeWithUnit.java

/**
 * Returns an object whose numeric value and unit are taken from the string
 * {@code sizeString}. If {@code sizeString} does not specify a unit and {@code defaultUnit} is not null,
 * {@code defaultUnit} is used as the unit. Null, empty or 'AUTO' string will produce {-1, SizeUnit#PIXELS}.
 *
 * @param sizeString  the string to be parsed
 * @param defaultUnit The unit to be used if {@code sizeString} does not contain any unit.
 *                    Use {@code null} for no default unit.
 * @return an object containing the parsed value and unit
 *//*ww  w  .ja  v  a2  s. c  om*/
public static SizeWithUnit parseStringSize(String sizeString, SizeUnit defaultUnit) {
    if (StringUtils.isEmpty(sizeString) || "auto".equalsIgnoreCase(sizeString)) {
        return new SizeWithUnit(-1, SizeUnit.PIXELS);
    }

    float size;
    SizeUnit unit;
    Matcher matcher = SIZE_PATTERN.matcher(sizeString);
    if (matcher.find()) {
        size = Float.parseFloat(matcher.group(1));
        if (size < 0) {
            size = -1;
            unit = SizeUnit.PIXELS;
        } else {
            String symbol = matcher.group(2);
            if ((symbol != null && symbol.length() > 0) || defaultUnit == null) {
                unit = SizeUnit.getUnitFromSymbol(symbol);
            } else {
                unit = defaultUnit;
            }
        }
    } else {
        throw new IllegalArgumentException(
                "Invalid size argument: \"" + sizeString + "\" (should match " + SIZE_PATTERN.pattern() + ")");
    }

    return new SizeWithUnit(size, unit);
}

From source file:com.thomaskuenneth.openweathermapweather.WeatherUtils.java

public static String getFromServer(String url) throws MalformedURLException, IOException {
    StringBuilder sb = new StringBuilder();
    URL _url = new URL(url);
    HttpURLConnection httpURLConnection = (HttpURLConnection) _url.openConnection();
    String contentType = httpURLConnection.getContentType();
    String charSet = "ISO-8859-1";
    if (contentType != null) {
        Matcher m = PATTERN_CHARSET.matcher(contentType);
        if (m.matches()) {
            charSet = m.group(1);
        }/*  w  ww .j  a  v a2  s . com*/
    }
    final int responseCode = httpURLConnection.getResponseCode();
    if (responseCode == HttpURLConnection.HTTP_OK) {
        InputStreamReader inputStreamReader = new InputStreamReader(httpURLConnection.getInputStream(),
                charSet);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            sb.append(line);
        }
        try {
            bufferedReader.close();
        } catch (IOException ex) {
            LOGGER.log(Level.SEVERE, "getFromServer()", ex);
        }
    }
    httpURLConnection.disconnect();
    return sb.toString();
}

From source file:de.tudarmstadt.ukp.clarin.webanno.brat.display.model.VID.java

public static VID parse(String aVid) {
    Matcher m = PATTERN_VID.matcher(aVid);
    if (m.matches()) {
        int annotationId = Integer.valueOf(m.group(1));
        int feature = NONE;
        int slot = NONE;
        if (m.group(2) != null) {
            feature = Integer.valueOf(m.group(2));
        }//from  w  ww  . j a va 2s. c o  m
        if (m.group(3) != null) {
            slot = Integer.valueOf(m.group(3));
        }
        return new VID(annotationId, feature, slot);
    } else {
        throw new IllegalArgumentException("Cannot parse visual identifier [" + aVid + "]");
    }
}

From source file:com.github.breadmoirai.samurai.plugins.groovyval.GroovyvalPlugin.java

public static String getFunctionName(String content) {
    final Matcher matcher = FUNCTION_NAME.matcher(content);
    if (matcher.find()) {
        return matcher.group(1);
    }//ww w . java2  s  .c  o  m
    return "";
}

From source file:mitm.common.util.AddressUtils.java

/**
 * Returns true if the address is a valid IPv4 or IPv6 network with optional CIDR netmask.
 * Examples: /*ww  w  .j  a va2s . c  om*/
 * 1) 192.168.0.0/32
 * 2) 127.0.0.1
 * 
 * @param address
 * @return
 */
public static boolean isValidNetwork(String network) {
    if (network == null) {
        return false;
    }

    boolean isValid = false;

    Matcher matcher = NETWORK_PATTERN.matcher(network);

    if (matcher.matches()) {
        String address = matcher.group(1);
        String cidr = matcher.group(3);

        if (isValidIPv4Address(address) || isValidIPv6Address(address)) {
            isValid = true;

            if (StringUtils.isNotEmpty(cidr)) {
                int bits = NumberUtils.toInt(cidr, -1);

                isValid = bits >= 0;
            }
        }
    }

    return isValid;
}

From source file:com.company.vertxstarter.api.util.SortExpression.java

public static SortExpression parse(String str) {
    Matcher m = PATTERN.matcher(str);
    if (!m.find()) {
        return null;
    }//from  w  ww  .  j a v a  2 s  . com

    SortExpression exp = new SortExpression();
    exp.setOrder(parseOrder(m.group(1)));
    if (exp.getOrder() == Order.NONE) {
        return null;
    }
    exp.setField(m.group(2));
    return exp;
}

From source file:com.magnet.plugin.helpers.UrlParser.java

public static ParsedUrl parseUrl(String url) {
    List<PathPart> pathParts = new ArrayList<PathPart>();
    List<Query> queries = new ArrayList<Query>();
    ParsedUrl parsedUrl;/*from   ww  w .j a v a2 s  .com*/
    String base;

    try {
        URL aURL = new URL(url);
        base = aURL.getAuthority();
        String protocol = aURL.getProtocol();
        parsedUrl = new ParsedUrl();
        parsedUrl.setPathWithEndingSlash(aURL.getPath().endsWith("/"));
        parsedUrl.setBase(protocol + "://" + base);
        List<NameValuePair> pairs = URLEncodedUtils.parse(aURL.getQuery(), Charset.defaultCharset());
        for (NameValuePair pair : pairs) {
            Query query = new Query(pair.getName(), pair.getValue());
            queries.add(query);
        }
        parsedUrl.setQueries(queries);

        String[] pathStrings = aURL.getPath().split("/");
        for (String pathPart : pathStrings) {
            Matcher m = PATH_PARAM_PATTERN.matcher(pathPart);
            if (m.find()) {
                String paramDef = m.group(1);
                String[] paramParts = paramDef.split(":");
                if (paramParts.length > 1) {
                    pathParts.add(new PathPart(paramParts[1].trim(), paramParts[0].trim()));
                } else {
                    pathParts.add(new PathPart(paramParts[0].trim()));
                }
            } else {
                if (!pathPart.isEmpty()) {
                    pathParts.add(new PathPart(pathPart));
                }
            }
        }
        parsedUrl.setPathParts(pathParts);
    } catch (Exception ex) {
        Logger.error(UrlParser.class, "Can't parse URL " + url);
        return null;
    }
    return parsedUrl;
}

From source file:Main.java

/**
 * Substitutes links in the given text with valid HTML mark-up. For instance,
 * http://dhis2.org is replaced with <a href="http://dhis2.org">http://dhis2.org</a>,
 * and www.dhis2.org is replaced with <a href="http://dhis2.org">www.dhis2.org</a>.
 *
 * @param text the text to substitute links for.
 * @return the substituted text.//from   www.j  a va 2  s  .c  om
 */
public static String htmlLinks(String text) {
    if (text == null || text.trim().isEmpty()) {
        return null;
    }

    Matcher matcher = LINK_PATTERN.matcher(text);

    StringBuffer buffer = new StringBuffer();

    while (matcher.find()) {
        String url = matcher.group(1);
        String suffix = matcher.group(3);

        String ref = url.startsWith("www.") ? "http://" + url : url;

        url = "<a href=\"" + ref + "\">" + url + "</a>" + suffix;

        matcher.appendReplacement(buffer, url);
    }

    return matcher.appendTail(buffer).toString();
}

From source file:cc.recommenders.mining.calls.QueryOptions.java

private static QueryType parseQueryType(String in) {
    Pattern p = compile(".*\\+Q\\[([A-Z0-9]+)\\].*");
    Matcher m = p.matcher(in);
    if (m.matches()) {
        return QueryType.valueOf(m.group(1));
    } else {//from  ww w  .j  a v a2  s. c om
        return QueryType.NM;
    }
}

From source file:lti.oauth.OAuthUtil.java

public static Map<String, String> decodeAuthorization(String authorization) {
    Map<String, String> oauthParameters = new HashMap<String, String>();
    if (authorization != null) {
        Matcher m = AUTHORIZATION.matcher(authorization);
        if (m.matches() && AUTH_SCHEME.equalsIgnoreCase(m.group(1))) {
            for (String keyValuePair : m.group(2).split("\\s*,\\s*")) {
                m = KEYVALUEPAIR.matcher(keyValuePair);
                if (m.matches()) {
                    String key = OAuthUtil.decodePercent(m.group(1));
                    String value = OAuthUtil.decodePercent(m.group(2));
                    oauthParameters.put(key, value);
                }/* w w  w. j av  a2 s  . c om*/
            }
        }
    }

    return oauthParameters;
}