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

/**
 * get innerHtml from href//  w ww  . ja v a 2 s.  c o  m
 * 
 * <pre>
 * getHrefInnerHtml(null)                                  = ""
 * getHrefInnerHtml("")                                    = ""
 * getHrefInnerHtml("mp3")                                 = "mp3";
 * getHrefInnerHtml("&lt;a innerHtml&lt;/a&gt;")                    = "&lt;a innerHtml&lt;/a&gt;";
 * getHrefInnerHtml("&lt;a&gt;innerHtml&lt;/a&gt;")                    = "innerHtml";
 * getHrefInnerHtml("&lt;a&lt;a&gt;innerHtml&lt;/a&gt;")                    = "innerHtml";
 * getHrefInnerHtml("&lt;a href="baidu.com"&gt;innerHtml&lt;/a&gt;")               = "innerHtml";
 * getHrefInnerHtml("&lt;a href="baidu.com" title="baidu"&gt;innerHtml&lt;/a&gt;") = "innerHtml";
 * getHrefInnerHtml("   &lt;a&gt;innerHtml&lt;/a&gt;  ")                           = "innerHtml";
 * getHrefInnerHtml("&lt;a&gt;innerHtml&lt;/a&gt;&lt;/a&gt;")                      = "innerHtml";
 * getHrefInnerHtml("jack&lt;a&gt;innerHtml&lt;/a&gt;&lt;/a&gt;")                  = "innerHtml";
 * getHrefInnerHtml("&lt;a&gt;innerHtml1&lt;/a&gt;&lt;a&gt;innerHtml2&lt;/a&gt;")        = "innerHtml2";
 * </pre>
 * 
 * @param href
 * @return <ul>
 *         <li>if href is null, return ""</li>
 *         <li>if not match regx, return source</li>
 *         <li>return the last string that match regx</li>
 *         </ul>
 */
public static String getHrefInnerHtml(String href) {
    if (isEmpty(href)) {
        return "";
    }

    String hrefReg = ".*<[\\s]*a[\\s]*.*>(.+?)<[\\s]*/a[\\s]*>.*";
    Pattern hrefPattern = Pattern.compile(hrefReg, Pattern.CASE_INSENSITIVE);
    Matcher hrefMatcher = hrefPattern.matcher(href);
    if (hrefMatcher.matches()) {
        return hrefMatcher.group(1);
    }
    return href;
}

From source file:Main.java

public static String getQueryPageUrl(String url, int inc) {
    if (url == null) {
        return null;
    }/*ww w  .  j  a v a  2s .c o m*/
    if (!url.contains("nicovideo.jp/search/")) {
        return null;
    }
    String ret = null;
    Matcher matcher = pagePattern.matcher(url);
    if (matcher.find()) {
        String _u1 = matcher.group(1);
        String _p = matcher.group(2);
        String _u2 = matcher.group(3);
        int page = 1;
        try {
            page = Integer.parseInt(_p) + inc;
        } catch (NumberFormatException e) {
        }
        if (0 < page) {
            ret = _u1 + page + _u2;
        }
    } else {
        int page = 1 + inc;
        if (0 < page) {
            if (url.contains("?")) {
                ret = url + "&page=" + page;
            } else {
                ret = url + "?page=" + page;
            }
        }
    }
    return ret;
}

From source file:com.github.terma.jenkins.githubprcoveragestatus.CoberturaParser.java

private static String findFirstOrNull(String string, String pattern) {
    Matcher matcher = Pattern.compile(pattern).matcher(string);
    if (matcher.find()) {
        return matcher.group(1);
    } else {/*from w w  w.jav a2s.co  m*/
        return null;
    }
}

From source file:musiccrawler.common.HtmlToolFix.java

public static String searchStreamLink(String script) {
    if (StringUtils.isNoneBlank(script)) {
        Pattern pattern = Pattern.compile(Constant.STREAM_LINK_PATTERN);
        Matcher matcher = pattern.matcher(script);
        if (matcher.find()) {
            return matcher.group(1).trim();
        }//from   w w  w .  j  ava2  s. c  om
    }
    return Constant.EMPTY;
}

From source file:Main.java

public static String getErrorMsg(String response) {
    Pattern errorPattern = Pattern.compile("<div class=\"problem\">(.*)</div>");
    Matcher errorMatcher = errorPattern.matcher(response);
    String errorContent;/*w w  w. j  a  v a 2s .co m*/
    if (errorMatcher.find()) {
        errorContent = errorMatcher.group(1).replaceAll("<[^>]+>", "");
    } else {
        errorContent = null;
    }

    return errorContent;
}

From source file:Main.java

/**
 * Get the total hrs logged this pay period.
 *
 * @param page the raw html of the user's timecard page
 * @return A double representing the total hours logged this pay period by the user.
 *///from   www .ja  v  a2  s  .  c  om
protected static double getTotalsHrs(String page) {
    double total = 0;

    try {
        Pattern pattern = Pattern.compile("(?i)(<div.*?>)(" + TOTAL_STR + ")(.*?)(</div>)");
        Matcher matcher = pattern.matcher(page);
        if (matcher.find()) {
            String totalStr = matcher.group(3);
            if (!(totalStr == null || totalStr.trim().length() == 0)) {
                total = Double.parseDouble(totalStr);
            }
        }
    } catch (Exception e) {
        Log.w(TAG, e.toString());
    }

    return total;
}

From source file:com.tek271.reverseProxy.utils.RegexTools.java

private static Tuple3<Integer, Integer, String> getMatch(Matcher matcher, int groupIndex) {
    return Tuple3.tuple3(matcher.start(groupIndex), matcher.end(groupIndex), matcher.group(groupIndex));
}

From source file:io.pivotal.kr.load_gen.Main.java

private static String stripSurroundingQuotes(String expr) {
    Matcher matcher = leadingQuotePattern.matcher(expr);

    if (matcher.matches()) {
        String leadingQuoteStripped = matcher.group(1);

        matcher = tailingQuotePattern.matcher(leadingQuoteStripped);

        if (matcher.matches()) {
            return matcher.group(1);
        } else {//from  w ww  . j a va 2s. c  o m
            return leadingQuoteStripped;
        }
    } else {
        return expr;
    }
}

From source file:Main.java

public static boolean possiblyWellformed(String in) {
    // extract starting element:
    in = in.trim();//  w  ww. j a v a 2s  .c o  m
    Matcher startMatcher = xmlStartPattern.matcher(in);
    if (startMatcher.find())
        return true;

    Matcher m = startingElementPattern.matcher(in);
    if (!m.matches())
        return false;
    String startingElementName = m.group(1);

    // check if fragment ends with the corresponding closing pattern:
    Pattern endPattern = Pattern.compile(".*</" + startingElementName + "\\s*>$");
    Matcher endMatcher = endPattern.matcher(in);

    return endMatcher.matches();
}

From source file:io.knotx.knot.service.service.ServiceAttributeUtil.java

private static String extract(String attributeName, int groupIndex) {
    Objects.requireNonNull(attributeName);

    Matcher matcher = ATTR_PATTERN.matcher(attributeName);
    if (matcher.matches()) {
        String namespace = matcher.group(groupIndex);
        return StringUtils.defaultString(namespace);
    } else {//from  w w  w .  ja v a  2 s  .c o  m
        throw new InvalidAttributeException(attributeName);
    }

}