Example usage for java.util.regex Matcher find

List of usage examples for java.util.regex Matcher find

Introduction

In this page you can find the example usage for java.util.regex Matcher find.

Prototype

public boolean find() 

Source Link

Document

Attempts to find the next subsequence of the input sequence that matches the pattern.

Usage

From source file:Main.java

/**
 * Get the last word (pure alphabet word) from a String
 * /*  w  w  w .  ja v a 2  s  . com*/
 * @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
 * /*from  w w  w.j a va2 s .co  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;
    }
}

From source file:Main.java

public static String parseIntToString(String value) {
    Matcher matcher = pattern.matcher(value);
    if (matcher.find()) {
        return matcher.group(0);
    }//from ww  w .ja v a2s.  com
    return null;
}

From source file:Main.java

public static boolean isEmailValid(String email) {
    boolean tag = true;
    final String pattern1 = "^([a-z0-9A-Z_]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
    final Pattern pattern = Pattern.compile(pattern1);
    final Matcher mat = pattern.matcher(email);
    if (!mat.find()) {
        tag = false;/*w w  w . ja  v  a2s.  c  o m*/
    }
    return tag;
}

From source file:Main.java

public static String getFileName(String fileName) {

    Pattern p_ext = Pattern.compile(pattern);
    Matcher m_ext = p_ext.matcher(fileName);

    if (m_ext.find()) {
        return fileName.substring(0, fileName.indexOf(m_ext.group()));
    } else {//  www  .  ja v a 2  s.c  o m
        return fileName;
    }
}

From source file:Main.java

public static boolean isValidEmail(String email) {
    boolean isValid = false;

    Pattern pattern = Pattern.compile(
            "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$");
    Matcher matcher = pattern.matcher(email);
    if (matcher.find()) {
        isValid = true;//  www. j av  a 2s . c  o  m
    }

    return isValid;
}

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 {/* w ww .j av  a  2s .  c  o  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 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));
    }//from   w ww  . jav a 2s  . c  om

    return -1;
}

From source file:Main.java

public static boolean isIDNumber(String id) {
    if (!TextUtils.isEmpty(id)) {
        Pattern pattern = Pattern
                .compile("^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9]|X)$");
        Matcher matcher = pattern.matcher(id);
        return matcher.find();
    }//from   w w w . j av  a2s . c  o  m
    return false;
}

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));
    }/* w ww . j  a v  a  2s .  co  m*/

    return -1;
}