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 String parseOptionalStringAttr(String line, Pattern pattern) {
    Matcher matcher = pattern.matcher(line);
    if (matcher.find()) {
        return matcher.group(1);
    }/* w ww  .j  a  v a 2s .  c o  m*/
    return null;
}

From source file:Main.java

public static List<String> matcherList(String regx, String content, int groupIndex) {
    List<String> ls = new ArrayList<String>();
    Pattern pattern = Pattern.compile(regx, Pattern.CASE_INSENSITIVE);
    Matcher matcher = pattern.matcher(content);
    while (matcher.find()) {
        ls.add(matcher.group(groupIndex));
    }/*  ww  w  . j  a  v  a 2s . com*/
    return ls;
}

From source file:Main.java

public static String getZipFromFixphone(String strNumber) {
    Matcher matcher = PATTERN_ZIPCODE.matcher(strNumber);
    if (matcher.find()) {
        return matcher.group(1);
    }//from w w w .  j a  v  a  2  s  . c o m

    return "";
}

From source file:Main.java

public static String searchForPattern(String message, String pattern) {

    Pattern first = Pattern.compile(pattern);
    Matcher match = first.matcher(message);

    if (match.find()) {
        String str = match.group(0);
        Log.i("searchForPattern", str);
        return str;
    }/*from w w w  .  j  a  v  a2 s.  c om*/
    Log.i("searchForPattern", "no match found");
    return null;
}

From source file:Main.java

public static String getDomain(String target) {
    Pattern p = Pattern.compile(".*?([^.]+\\.[^.]+)");
    URI uri;//from  ww  w  . j  a  v a 2 s  .c om

    try {
        uri = new URI(target);

    } catch (Exception e) {
        return "http";
    }

    String host = uri.getHost();
    Matcher m = p.matcher(host);

    if (m.matches())
        return m.group(1);
    else
        return host;
}

From source file:Main.java

private static String getFileName(HttpURLConnection conn) {
    String fileName = conn.getHeaderField("Content-Disposition");
    Matcher matcher = REGEX_FILE_NAME.matcher(fileName);
    if (matcher.find()) {
        return matcher.group(1);
    } else {/*from  www  .  j a  v  a  2  s  .co m*/
        return null;
    }
}

From source file:GrepNIO.java

static void process(Pattern pattern, String fileName) throws IOException {

    // Get a FileChannel from the given file.
    FileChannel fc = new FileInputStream(fileName).getChannel();

    // Map the file's content
    ByteBuffer buf = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());

    // Decode ByteBuffer into CharBuffer
    CharBuffer cbuf = Charset.forName("ISO-8859-1").newDecoder().decode(buf);

    Matcher m = pattern.matcher(cbuf);
    while (m.find()) {
        System.out.println(m.group(0));
    }// ww  w  .ja v  a  2 s  .c om
}

From source file:Main.java

public static int getCurrentPage(String url) {
    Matcher matcher = PAGE_PATTERN.matcher(url);
    if (matcher.find()) {
        return Integer.parseInt(matcher.group(1));
    }/*from   www .  ja  va 2  s  . c o  m*/
    return 0;
}

From source file:Main.java

public static ArrayList<String> getResultUrl(String html) {
    ArrayList<String> resultUrl = 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()) {
        resultUrl.add(matcher.group(1));
        System.out.println(matcher.group(1));

    }/*  w  ww.  j av  a2 s. c om*/

    return resultUrl;
}

From source file:Main.java

public static String getFirstMatch(String html, String pattern) {
    Pattern p = Pattern.compile(pattern);
    Matcher m = p.matcher(html);

    String result = "";
    while (m.find()) {
        result = m.group(1);
        break;/*from ww  w . j av  a2 s.  c o  m*/
    }
    return result;
}