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:MainClass.java

public static void main(String args[]) {
    Pattern p = Pattern.compile("Java");

    String candidateString_1 = "Java is Java";
    String candidateString_2 = "J2SE is Java";
    String candidateString_3 = "J2SEisJava";

    Matcher matcher = p.matcher(candidateString_1);
    String msg = ":" + candidateString_1 + ": matches?: ";
    System.out.println(msg + matcher.lookingAt());

    matcher.reset(candidateString_2);//  w  w w  . j  a  v  a  2 s .  co m
    msg = ":" + candidateString_2 + ": matches?: ";
    System.out.println(msg + matcher.lookingAt());

    matcher.reset(candidateString_3);
    msg = ":" + candidateString_3 + ": matches?: ";
    System.out.println(msg + matcher.lookingAt());

}

From source file:Main.java

public static void main(String[] args) {
    String regex = "[a-z]@.";
    Pattern p = Pattern.compile(regex);
    String str = "abc@yahoo.com,123@cnn.com,abc@google.com";
    Matcher m = p.matcher(str);/*  w  ww.  j  a  va2s .c o  m*/
}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    Pattern pattern = Pattern.compile("pattern");
    Matcher matcher = pattern.matcher("infile.txt");

    // Find all matches
    while (matcher.find()) {
        // Get the matching string
        String match = matcher.group();
    }//w  ww.ja v a2s  .  c o  m
}

From source file:MainClass.java

public static void main(String args[]) {
    Pattern p = Pattern.compile("J2SE");

    String candidateString_1 = "j2se";
    String candidateString_2 = "J2SE ";
    String candidateString_3 = "J2SE";

    Matcher matcher_1 = p.matcher(candidateString_1);
    Matcher matcher_2 = p.matcher(candidateString_2);
    Matcher matcher_3 = p.matcher(candidateString_3);

    String msg = ":" + candidateString_1 + ": matches?: ";
    System.out.println(msg + matcher_1.matches());

    msg = ":" + candidateString_2 + ": matches?: ";
    System.out.println(msg + matcher_2.matches());

    msg = ":" + candidateString_3 + ": matches?: ";
    System.out.println(msg + matcher_3.matches());
}

From source file:Main.java

public static void main(String[] args) {
    String str = "_6654_1";

    Pattern p = Pattern.compile("_(\\d+)_1");
    Matcher m = p.matcher(str);//ww w  .j a v a  2  s.c o  m
    if (m.matches())
        System.out.println(m.group(1)); // prints 6654
}

From source file:Main.java

public static void main(String[] args) {
    String word = "77933211";
    Pattern pattern = Pattern.compile("(\\d)\\1");
    Matcher matcher = pattern.matcher(word);
    word = matcher.replaceAll("$1");
    System.out.println(word);/*w  w  w. jav a2  s.  com*/
}

From source file:MainClass.java

public static void main(String args[]) {
    // create a Pattern
    Pattern p = Pattern.compile("Bond");

    // create a Matcher and use the Matcher.group() method
    String candidateString = "My name is Bond. James Bond.";
    Matcher matcher = p.matcher(candidateString);
    // extract the group
    matcher.find();/*from w w w.  j a v  a  2s .c o  m*/
    System.out.println(matcher.group());
}

From source file:Main.java

public static void main(String[] args) {
    String regex = "[a-z]@.";
    Pattern p = Pattern.compile(regex);
    String str = "abc@yahoo.com,123@cnn.com,abc@google.com";
    Matcher m = p.matcher(str);// www .  j a va  2 s. c  o  m

    if (m.find()) {
        String foundStr = m.group();
        System.out.println("Found text is:" + foundStr);
    }
}

From source file:MatcherMatchesExample.java

public static void main(String args[]) {
    Pattern p = Pattern.compile("J2SE");

    String candidateString_1 = "j2se";
    String candidateString_2 = "J2SE ";
    String candidateString_3 = "J2SE2s";

    Matcher matcher_1 = p.matcher(candidateString_1);
    Matcher matcher_2 = p.matcher(candidateString_2);
    Matcher matcher_3 = p.matcher(candidateString_3);

    String msg = ":" + candidateString_1 + ": matches?: ";
    System.out.println(msg + matcher_1.matches());

    msg = ":" + candidateString_2 + ": matches?: ";
    System.out.println(msg + matcher_2.matches());

    msg = ":" + candidateString_3 + ": matches?: ";
    System.out.println(msg + matcher_3.matches());

}

From source file:Main.java

public static void main(String args[]) {
    // match a single digit
    Pattern p = Pattern.compile("\\d");
    Matcher matcher = p.matcher("5");
    boolean isOk = matcher.matches();
    System.out.println("original pattern matches " + isOk);

    // recycle the pattern
    String tmp = p.pattern();//from w  ww. j  a va2 s  . c o m
    Pattern p2 = Pattern.compile(tmp);
    matcher = p.matcher("5");
    isOk = matcher.matches();
    System.out.println("second pattern matches " + isOk);
}