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 ArrayList<String> getResultUrl(String html) {
    ArrayList<String> resultUrl = new ArrayList<String>();
    String re = "<h3 class=\\\\\"r\\\\\"(.+?)>(.+?)<a href=\\\\\"(.+?)\\\\\" target=\\\\\"_blank\\\\\">(.+?)<\\\\/a>";
    System.out.println(re);//from   w ww.j  a v  a 2 s .  c om
    Pattern pattern = Pattern.compile(re);
    Matcher matcher = pattern.matcher(html);
    while (matcher.find()) {
        resultUrl.add(matcher.group(3).replace("\\/", "/").replace("&amp;", "&").replace("&amp;", "&"));
        System.out.println(matcher.group(3).replace("\\/", "/").replace("&amp;", "&").replace("&amp;", "&"));

    }

    return resultUrl;
}

From source file:Main.java

public static String getOneStrFromString(String strPattern, String content) {
    try {/* w  w w  .jav a 2 s. c  o m*/
        Pattern pattern = Pattern.compile(strPattern);
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            return matcher.group(1);
        }
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return "";
}

From source file:Main.java

public static Document stringToDocument(final String source) {
    String tmp = source.trim();//from w  ww  .  ja v a2 s.c  om
    if (!tmp.startsWith("<?xml")) {
        tmp = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + tmp;
    }
    String encode = "utf-8";
    Pattern p = Pattern.compile("<?.*encoding=\"([^ ]*)\".*?>");
    Matcher m = p.matcher(tmp);
    if (m.find()) {
        encode = m.group(1);
    }
    try {
        return DocumentBuilderFactory.newInstance().newDocumentBuilder()
                .parse(new ByteArrayInputStream(tmp.getBytes(encode)));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String getDateFromString(String content) {
    try {//from w ww.j  a va 2s  . c  om
        String strPattern = "DB_PoliceOfficeWork_(\\d{4}-\\d{2}-\\d{2}).db";
        Pattern pattern = Pattern.compile(strPattern);
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            return matcher.group(1);
        }
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return "";
}

From source file:Main.java

public static int getNumberOfRejectedEvents(String response) {
    String patternString = "\"rej\":(\\d*)";
    Pattern pattern = Pattern.compile(patternString);
    Matcher matches = pattern.matcher(response);
    if (matches.find()) {
        return Integer.parseInt(matches.group(1));
    }// w w  w. j  a  v  a 2  s.c om

    return -1;
}

From source file:Main.java

public static int getNumberOfAcceptedEvents(String response) {
    String patternString = "\"acc\":(\\d*)";
    Pattern pattern = Pattern.compile(patternString);
    Matcher matches = pattern.matcher(response);
    if (matches.find()) {
        return Integer.parseInt(matches.group(1));
    }/*from   w  w w .  j  ava  2s  . c o m*/

    return -1;
}

From source file:Main.java

/**
 * Extract namespaces into a map for adding to the builder.
 * E.g. xmlns:xsd="http://www.w3.org/2001/XMLSchema"
 * @param xml XML source//from ww  w  .ja  va2  s .c o  m
 * @return map of namespaces : URL, e.g. [ "xsd" : "http://www.w3.org/2001/XMLSchema" ]
 */
private static Map<String, String> parseNamespaces(String xml) {

    Map<String, String> nsMap = new HashMap<String, String>();

    Pattern nsPat = Pattern.compile("xmlns:(\\w+)=\"([^\"]+)\"");
    Matcher nsMat = nsPat.matcher(xml);

    while (nsMat.find()) {
        String ns = nsMat.group(1);
        String url = nsMat.group(2);
        nsMap.put(ns, url);
    }

    return nsMap;
}

From source file:Main.java

public static ArrayList<String> getResultContent(String html) {
    ArrayList<String> resultContent = new ArrayList<String>();
    String re = "<div class=\"sort_name_detail\"><a href=\"(.+?)\" target=\"_blank\" title=\"(.+?)\">";
    Pattern pattern = Pattern.compile(re);
    Matcher matcher = pattern.matcher(html);
    while (matcher.find()) {
        resultContent.add(matcher.group(2).replace("\n", "").replace(" ", "").replace("\t", "")
                .replace("<b>", "").replace("</b>", ""));
        System.out.println(matcher.group(2));

    }/*from  w w  w.  j  a  v a2 s .  c om*/

    return resultContent;
}

From source file:Main.java

public static String getContentMathML(String latexMLResponse) {
    String CML_REGEX = "<annotation-xml.*MWS-Query\">(.*)<.annotation-xml>";
    Pattern pattern = Pattern.compile(CML_REGEX, Pattern.DOTALL);

    try {//from   w ww. jav  a 2  s  . com
        JSONObject latexMLJSON = new JSONObject(latexMLResponse);
        String math = latexMLJSON.getString("result");

        Matcher matcher = pattern.matcher(math);
        if (matcher.find()) {
            return matcher.group(1);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String extractTitle(String html) {
    if (html == null) {
        return null;
    }/*from   ww w  .j ava2  s .com*/

    Matcher matcher = patternTitle.matcher(html);
    if (matcher.find()) {
        String title = matcher.group(1);
        if (title != null) {
            title = title.trim();
        }
        return title;
    }
    return null;
}