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.sun.socialsite.util.RegexUtil.java

/**
 * Return the specified match "groups" from the pattern.
 * For each group matched a String will be entered in the ArrayList.
 *
 * @param pattern The Pattern to use./*from  w  w  w  .  j a  v  a 2s  . c o m*/
 * @param match The String to match against.
 * @param group The group number to return in case of a match.
 * @return
 */
public static List<String> getMatches(Pattern pattern, String match, int group) {
    List<String> matches = new ArrayList<String>();
    Matcher matcher = pattern.matcher(match);
    while (matcher.find()) {
        matches.add(matcher.group(group));
    }
    return matches;
}

From source file:org.syphr.mythtv.ws.impl.ServiceUtils.java

public static String getVersion(URI serviceBaseUri) throws IOException {
    URI uri = URI.create(serviceBaseUri.toString() + "/" + VERSION_URI_PATH);

    HttpClient httpclient = new DefaultHttpClient();
    try {//from  ww w  .  j  a v  a  2s.  c  om
        HttpGet httpget = new HttpGet(uri);
        LOGGER.debug("Retrieving service version from {}", httpget.getURI());

        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String responseBody = httpclient.execute(httpget, responseHandler);
        LOGGER.trace("Version response: {}", responseBody);

        Matcher matcher = VERSION_PATTERN.matcher(responseBody);
        if (matcher.matches()) {
            return matcher.group(1);
        }

        throw new IOException("Failed to retrieve version information");
    } finally {
        httpclient.getConnectionManager().shutdown();
    }
}

From source file:cn.cuizuoli.gotour.utils.HtmlHelper.java

/**
 * toList/* ww  w  .  j  a v a2  s. c o  m*/
 * @param text
 * @return
 */
public static String toList(String text) {
    String[] lines = StringUtils.split(text, "\r\n");
    StringBuffer listBuffer = new StringBuffer();
    listBuffer.append("<ul>").append("\n");
    for (String line : lines) {
        Matcher listMatcher = LIST_PATTERN.matcher(line);
        if (listMatcher.matches()) {
            String li = listMatcher.group(1) + listMatcher.group(2);
            if (StringUtils.contains(li, "")) {
                li = "<strong>" + StringUtils.replace(li, "", "</strong>");
            }
            listBuffer.append("<li>").append(li).append("</li>").append("\n");
        }
    }
    listBuffer.append("</ul>").append("\n");
    return listBuffer.toString();
}

From source file:Main.java

/**
 * Parses an RFC 5545 UTC offset and returns milliseconds.
 * //from  w  ww .jav  a 2 s.c o  m
 * Grammar:
 *        utc-offset = time-numzone
 *        time-numzone = ("+" / "-") time-hour time-minute [time-second]
 */
public static int parseUtcOffset(String value) {
    final Pattern utcOffsetPattern = Pattern.compile("([-+])(\\d{1,2})(\\d{2})(?:\\d{2})?");
    final Matcher matcher = utcOffsetPattern.matcher(value);
    if (!matcher.matches()) {
        throw new IllegalArgumentException(value);
    }
    int sign = matcher.group(1).equals("+") ? 1 : -1;
    int hours = Integer.parseInt(matcher.group(2));
    int minutes = Integer.parseInt(matcher.group(3));
    int offsetSeconds = sign * (hours * 60 + minutes) * 60;
    return offsetSeconds * 1000;
}

From source file:Main.java

/**
 * Extract the UUID part from a MusicBrainz identifier.
 * // w w  w.j a v a 2  s  . co m
 * This function takes a MusicBrainz ID (an absolute URI) as the input
 * and returns the UUID part of the URI, thus turning it into a relative
 * URI. If <code>uriStr</code> is null or a relative URI, then it is
 * returned unchanged.
 * 
 * The <code>resType</code> parameter can be used for error checking.
 * Set it to 'artist', 'release', or 'track' to make sure 
 * <code>uriStr</code> is a syntactically valid MusicBrainz identifier
 * of the given resource type. If it isn't, an 
 * <code>IllegalArgumentException</code> exception is raised. This error
 * checking only works if <code>uriStr</code> is an absolute URI, of course.
 * 
 * Example:
 * >>>  MBUtils.extractUuid('http://musicbrainz.org/artist/c0b2500e-0cef-4130-869d-732b23ed9df5', 'artist')
 * 'c0b2500e-0cef-4130-869d-732b23ed9df5'
 * 
 * @param uriStr A string containing a MusicBrainz ID (an URI), or null
 * @param resType A string containing a resource type
 * @return A String containing a relative URI or null
 * @throws URISyntaxException 
 */
public static String extractUuid(String uriStr, String resType) {
    if (uriStr == null) {
        return null;
    }

    URI uri;
    try {
        uri = new URI(uriStr);
    } catch (URISyntaxException e) {
        return uriStr; // not really a valid URI, probably the UUID
    }
    if (uri.getScheme() == null) {
        return uriStr; // not really a valid URI, probably the UUID
    }

    if (!"http".equals(uri.getScheme()) || !"musicbrainz.org".equals(uri.getHost())) {
        throw new IllegalArgumentException(uri.toString() + " is no MB ID");
    }

    String regex = "^/(label|artist|release-group|release|recording|work|collection)/([^/]*)$";
    Pattern p = Pattern.compile(regex);
    Matcher m = p.matcher(uri.getPath());
    if (m.matches()) {
        if (resType == null) {
            return m.group(2);
        } else {
            if (resType.equals(m.group(1))) {
                return m.group(2);
            } else {
                throw new IllegalArgumentException("expected '" + resType + "' Id");
            }
        }
    } else {
        throw new IllegalArgumentException("'" + uriStr + " is no valid MB id");
    }
}

From source file:com.qumoon.commons.TaobaoUtils.java

/**
 * ?? url ? num iid/*from   ww w . ja v a2s  .  c  o m*/
 */
public static Long getItemUrlNumIid(String url) {
    Matcher matcher = ITEM_NUM_IID_PATTERN.matcher(url);
    if (matcher.find() && matcher.groupCount() > 1) {
        String numIidStr = matcher.group(2);
        if (NumberUtils.isNumber(numIidStr)) {
            return Long.valueOf(numIidStr);
        } else {
            return null;
        }
    }
    return null;
}

From source file:edu.harvard.i2b2.fhirserver.ws.I2b2Helper.java

static String extractPatientId2(String string, String resourceName) {
    logger.debug("requestUrl is:" + string);
    if (string == null)
        return null;
    String id = null;//from w w w  . j ava 2s  .c o m

    Pattern p = Pattern.compile(".*/([a-zA-Z0-9]+)[-]*.*$");
    Matcher m = p.matcher(string);

    if (m.find()) {
        id = m.group(1);
        logger.trace("id:" + id);
    }
    if (id.equals(resourceName))
        id = null;
    return id;
}

From source file:fedroot.dacs.examples.ExampleRunner.java

private static void getCookiesWithEmail() {
    //        Pattern name = Pattern.compile("(DACS:[:\\w]+)=([-\\w]+)");
    //        Matcher m = name.matcher(cookieHeader);
    //        while (m.find()) {
    //            String cookieName = m.group(1);
    //            String cookieValue = m.group(2);
    //        }/*  ww w  .  j  a  va2s .com*/

    //        String test = "DACS:NFIS::CA:roderick.oneil-morrison@fedroot.com";
    String test = "JSESSIONID=fec51a10f955a26756e4d15d6eb2; DACS:FEDROOT::METALOGIC:rmorriso@fedroot.com=mIIKA5vrtLJgMxTG3UsrS4FBbQopZk1gxG4lDmuOUCL2w53n3wYoX2vdPBiF1K1xKOaEsyw3arq0PBnrxWNDLW0IP_O1jVJGAO14gWUNgkIFaEpdGLY3vOnXdZmCjYDTKLqAdzTJDm8GSHBjzr-XZvfokf_yrOnkYpFrxpZQgACEgMnPamqO4BUMeZcbWqo1_4TjxmzM5gWLKu1y0KwltG8QVLqfc4cCWfnakQuIT9VNDRnoyi79lh-RhIMugJGRJcICwlTEi5nlusrucooAk7_PP0kCEh2FMGJb03GR3Cj-yf6Ayh87KZpOuSNYPCyrmxW030bVLbsVHIlBdeMyvpmz5xJkqu-jfAuINlgqmKdY1p6jYkxsijI2s2lTJetIpkZnocnbSvU_RQSMezhLQWeVQu02clhDusMvjv3bMWfwNU7CiDhowXq8cSly5mLH6GhKH7iyP0; DACS:FEDROOT::DSS:brachman=mIIKA5vrtLJgMxTG3UsrS4FBbQopZk1gxG4lDmuOUCL2w53n3wYoX2vdPBiF1K1xKOaEsyw3arq0PBnrxWNDLW0IP_O1jVJGAO14gWUNgkIFaEpdGLY3vOnXdZmCjYDTKLqAdzTJDm8GSHBjzr-XZvfokf_yrOnkYpFrxpZQgACEgMnPamqO4BUMeZcbWqo1_4TjxmzM5gWLKu1y0KwltG8QVLqfc4cCWfnakQuIT9VNDRnoyi79lh-RhIMugJGRJcICwlTEi5nlusrucooAk7_PP0kCEh2FMGJb03GR3Cj-yf6Ayh87KZpOuSNYPCyrmxW030bVLbsVHIlBdeMyvpmz5xJkqu-jfAuINlgqmKdY1p6jYkxsijI2s2lTJetIpkZnocnbSvU_RQSMezhLQWeVQu02clhDusMvjv3bMWfwNU7CiDhowXq8cSly5mLH6GhKH7iyP0; JSESSIONID=fec51a10f955a26756e4d15d6eb2";
    Pattern email = Pattern.compile(
            "(DACS:[:\\w]+[\\w][\\w\\-]*[\\.]{0,1}[\\w\\-]*[@[A-Za-z0-9-]+\\.+[A-Za-z]{2,4}]*)=([-\\w]+)",
            Pattern.CASE_INSENSITIVE);
    Matcher emailMatcher = email.matcher(test);
    while (emailMatcher.find()) {
        String group1 = emailMatcher.group(1);
        String group2 = emailMatcher.group(2);
    }
}

From source file:com.epam.catgenome.controller.vo.externaldb.NCBIVariationVO.java

private static String regexExtract(Pattern p, String sourceString) {
    String result = null;// ww w  . j av a  2  s  . c  o  m
    Matcher m = p.matcher(sourceString);

    if (m.matches()) {
        result = m.group(1);
    }

    return result;
}

From source file:net.fabricmc.installer.installer.MultiMCInstaller.java

private static String stripDepsJson(String input) {
    Pattern pattern = Pattern.compile("\\[(.*?)\\]");
    Matcher matcher = pattern.matcher(input);
    matcher.find();/*from  w  w w . j av  a 2  s  .c o  m*/
    return matcher.group(1);
}