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 List<String> getStringDigit(String text) {
    text = null == text ? "" : text;
    List<String> digitList = new ArrayList<String>();
    Pattern p = Pattern.compile("(\\d+)");
    Matcher m = p.matcher(text);
    while (m.find()) {
        String find = m.group(1).toString();
        digitList.add(find);//w  w  w .j a  va2s  . co  m
    }
    return digitList;
}

From source file:Main.java

public static String extractMatchString(String regex, String target) {
    Pattern pattern = Pattern.compile(regex);
    Matcher matcher = pattern.matcher(target);
    if (matcher.find()) {
        return matcher.group(1);
    } else {/* w  w w  . j  a  v a2 s  . c om*/
        return null;
    }
}

From source file:Main.java

public static String getCsFromRE(String pt, String content) {
    Pattern pa1 = Pattern.compile(pt, Pattern.CASE_INSENSITIVE);
    Matcher matcher1 = pa1.matcher(content);
    if (matcher1.find()) {
        return matcher1.group(1);
    } else {//from   w ww.  j  a va2 s.com
        return "";
    }
}

From source file:Main.java

public static String readValue(String whole, String key) {
    Pattern pattern = Pattern.compile(key + "=([\"'])(.+?)\\1", Pattern.CASE_INSENSITIVE);
    Matcher mat = pattern.matcher(whole);
    if (mat.find()) {
        return mat.group(2);
    } else {//from  w w  w.  j  a  v  a  2  s.  co  m
        pattern = Pattern.compile(key + "=([^ \t\r\n\f\b\"'/>]+?)[ \t\r\n\f\b\"'/>]", Pattern.CASE_INSENSITIVE);
        mat = pattern.matcher(whole);
        if (mat.find()) {
            return mat.group(1);
        }
    }
    return "";
}

From source file:Main.java

public static Map<String, String> parseCommaDelimitedProps(String s) {
    if (s == null)
        return null;
    Map<String, String> props = new HashMap<String, String>();
    Pattern p = Pattern.compile("\\s*([^=\\s]+)\\s*=\\s*([^=\\s,]+)\\s*,?"); //Pattern.compile("\\s*([^=\\s]+)\\s*=\\s([^=\\s]+)\\s*,?");
    Matcher matcher = p.matcher(s);
    while (matcher.find()) {
        props.put(matcher.group(1), matcher.group(2));
    }//  w w  w.jav  a  2s . c o  m
    return props;
}

From source file:Main.java

public static String getImgUrlFromHtml(String html) {
    String result = "";
    String imgRegex = "<img[^>]+src\\s*=\\s*['\"]([^'\"]+)['\"][^>]*>";
    Pattern pattern = Pattern.compile(imgRegex);
    Matcher matcher = pattern.matcher(html);
    if (matcher.find()) {
        result = matcher.group(2);
    }/*  ww  w.  ja  v  a  2 s  . co  m*/
    return result;
}

From source file:Main.java

public static String parseIntToString(String value) {
    Matcher matcher = pattern.matcher(value);
    if (matcher.find()) {
        return matcher.group(0);
    }//from w  w w . j  ava 2 s  . c o m
    return null;
}

From source file:Main.java

public static String splitDate(String pattern, String url) {
    String date = "";
    Matcher m = Pattern.compile(pattern).matcher(url);

    if (m.find()) {
        date = m.group(1);
    }//from  w  ww  .  j  a  v a  2 s. c o m

    return date;
}

From source file:Main.java

/**
 * Get the last word (pure alphabet word) from a String
 * /*from   www.j  a v a 2 s.c  om*/
 * @param source
 *            the string where the last word is to be extracted
 * @return the extracted last word or null if there is no last word
 */
public static String getLastWord(String source) {
    if (source == null) {
        return null;
    }

    source = source.trim();

    if (source.isEmpty()) {
        return null;
    }

    if (containsSpace(source)) {
        final int LAST_WORD_GROUP = 1;
        String lastWordRegex = "\\s([A-z]+)[^A-z]*$";
        Pattern pattern = Pattern.compile(lastWordRegex);
        Matcher matcher = pattern.matcher(source);
        if (matcher.find()) {
            return matcher.group(LAST_WORD_GROUP);
        } else {
            return null;
        }

    } else {
        return source;
    }
}

From source file:Main.java

/**
 * Get the first word (pure alphabet word) from the String
 * //w  w  w . j a va  2s  . c  o  m
 * @param source
 *          the string where the first word is to be extracted
 * @return the extracted first word or null if there is no first word
 */
public static String getFirstWord(String source) {
    if (source == null) {
        return null;
    }

    source = source.trim();

    if (source.isEmpty()) {
        return null;
    }

    if (containsSpace(source)) {

        final int FIRST_WORD_GROUP = 1;
        String firstWordRegex = "^[^A-z]*([A-z]+)";
        Pattern pattern = Pattern.compile(firstWordRegex);
        Matcher matcher = pattern.matcher(source);
        if (matcher.find()) {
            return matcher.group(FIRST_WORD_GROUP);
        } else {
            return null;
        }
    } else {
        return source;
    }
}