Example usage for java.util.regex Pattern compile

List of usage examples for java.util.regex Pattern compile

Introduction

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

Prototype

public static Pattern compile(String regex) 

Source Link

Document

Compiles the given regular expression into a pattern.

Usage

From source file:Main.java

public static boolean isInteger(String str) {
    if (str == null)
        return false;
    Pattern pattern = Pattern.compile("[0-9]+");
    return pattern.matcher(str).matches();
}

From source file:Main.java

public static String getVerifyCodeFromSms(String smsBody) {
    Pattern pattern = Pattern.compile("\\d{6}");
    Matcher matcher = pattern.matcher(smsBody);
    if (matcher.find()) {
        return matcher.group();
    }//  www .j a  v a 2s. c o m
    return null;
}

From source file:Main.java

public static String extractPattern(String string, String pattern) {
    Pattern p = Pattern.compile(pattern);
    Matcher m = p.matcher(string);
    if (!m.find())
        return null;
    return m.toMatchResult().group(1);
}

From source file:Main.java

public static boolean eMailValidation(String emailstring) {
    Pattern emailPattern = Pattern.compile("[a-zA-Z0-9\\+\\.\\_\\%\\-\\+]{1,256}" + "\\@"
            + "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}" + "(" + "\\." + "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,25}" + ")+");
    Matcher emailMatcher = emailPattern.matcher(emailstring);
    return emailMatcher.matches();
}

From source file:Main.java

public static boolean MatchRegExp(String _str, String _regexp) {
    Pattern pattern = Pattern.compile(_regexp);
    Matcher matcher = pattern.matcher(_str);
    boolean matched = matcher.matches();
    return matched;
}

From source file:Main.java

private static String extractPattern(String string, String pattern) {
    Pattern p = Pattern.compile(pattern);
    Matcher m = p.matcher(string);

    if (!m.find()) {
        return null;
    }//from ww w.j  ava2s  .  com
    return m.toMatchResult().group(1);
}

From source file:Main.java

public static boolean isMatcherStr(String rex, String data) {
    Pattern pattern = Pattern.compile(rex);
    Matcher isNum = pattern.matcher(data);

    if (isNum.matches()) {
        return true;
    }// w w  w  .  j  av a2s .  com
    return false;
}

From source file:Main.java

public static String decodeXML(String inXML) {
    Pattern p1 = Pattern.compile("<");
    Matcher m1 = p1.matcher(inXML); // get a matcher object
    StringBuffer sb1 = new StringBuffer();
    while (m1.find()) {
        m1.appendReplacement(sb1, "<");
    }/*from   w  w  w.j a  va  2 s  .c om*/
    inXML = m1.appendTail(sb1).toString();

    Pattern p2 = Pattern.compile("&gt;");
    Matcher m2 = p2.matcher(inXML); // get a matcher object
    StringBuffer sb2 = new StringBuffer();
    while (m2.find()) {
        m2.appendReplacement(sb2, ">");
    }
    String outXML = m2.appendTail(sb2).toString();

    return outXML;
}

From source file:Main.java

public static String encodeXML(String inXML) {
    Pattern p1 = Pattern.compile("<");
    Matcher m1 = p1.matcher(inXML); // get a matcher object
    StringBuffer sb1 = new StringBuffer();
    while (m1.find()) {
        m1.appendReplacement(sb1, "&lt;");
    }/*from   w  w  w .  j av a  2s. c om*/
    inXML = m1.appendTail(sb1).toString();

    Pattern p2 = Pattern.compile(">");
    Matcher m2 = p2.matcher(inXML); // get a matcher object
    StringBuffer sb2 = new StringBuffer();
    while (m2.find()) {
        m2.appendReplacement(sb2, "&gt;");
    }
    String outXML = m2.appendTail(sb2).toString();

    return outXML;
}

From source file:Main.java

public static String getFirstMatch(String html, String pattern) {
    Pattern p = Pattern.compile(pattern);
    Matcher m = p.matcher(html);//from w  w  w .  java2s .  co m

    String result = "";
    while (m.find()) {
        result = m.group(1);
        break;
    }
    return result;
}