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

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   ww w  .jav  a  2 s .  com*/
    Log.i("searchForPattern", "no match found");
    return null;
}

From source file:Main.java

/**
 * Search for patterns [key=value] and returns the value given the key.
 * //from w  ww . j av  a2s  .  co  m
 * @param key
 * @param string
 * @return
 */
public static String lookupParameterValue(String key, String string) {
    Pattern p = Pattern.compile("\\[" + key + "=[^\\]]*\\]");
    Matcher m = p.matcher(string);
    m.find();
    String f = m.group();
    int p1 = f.indexOf('=');
    int p2 = f.indexOf(']');
    return f.substring(p1 + 1, p2);
}

From source file:Main.java

/**
 * Search the first matched string/*from ww  w . jav  a 2s . co  m*/
 * @param str
 * @param regex
 * @return
 */
public static String getMatchedString(String str, String regex) {
    String ret = null;

    Pattern pattern = Pattern.compile(regex);
    Matcher matcher = pattern.matcher(str);
    if (matcher.find()) {
        ret = matcher.group();
    }

    return ret;
}

From source file:Main.java

public static boolean isPhoneNumber(String phoneNumber) {
    if (!TextUtils.isEmpty(phoneNumber)) {
        String regex = "1([\\d]{10})|((\\+[0-9]{2,4})?\\(?[0-9]+\\)?-?)?[0-9]{7,8}";
        Pattern p = Pattern.compile(regex);
        Matcher matcher = p.matcher(phoneNumber);
        return matcher.find();
    } else {//w  ww .java2  s.  c  om
        return false;
    }
}

From source file:Main.java

public static boolean isQQNumber(String qqNumber) {
    if (!TextUtils.isEmpty(qqNumber)) {
        Pattern pattern = Pattern.compile("\\d{4,12}$");
        Matcher matcher = pattern.matcher(qqNumber);
        return matcher.find();
    }//from   w w w .  j a  va  2s. c  o m
    return false;
}

From source file:Main.java

private static boolean isMasterCard(String creditCardNumber) {

    Pattern pattern = Pattern.compile(MasterCardCreditCardPattern);
    Matcher matcher = pattern.matcher(creditCardNumber);
    return matcher.find();
}

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  w w .j  av  a 2  s  .c om*/
    return null;
}

From source file:Main.java

public static boolean isUserNameCorrect(String userName) {
    if (!TextUtils.isEmpty(userName)) {
        Pattern pattern = Pattern.compile("^[0-9a-zA-Z_]{6,20}$");
        Matcher matcher = pattern.matcher(userName);
        return matcher.find();
    }/*from  w ww  .j a  va 2  s.co m*/
    return false;
}

From source file:Main.java

public static String parseOptionalStringAttr(String line, Pattern pattern) {
    Matcher matcher = pattern.matcher(line);
    if (matcher.find() && matcher.groupCount() == 1) {
        return matcher.group(1);
    }/*from w ww  .  j a v  a  2s.  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));
    }/*from  w  w  w .  j  av  a 2 s.  c om*/
}