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) {
    Matcher m = Pattern.compile("\\w+").matcher("Today is Sunday");
    while (m.find())
        System.out.println(m.group());
    int i = 0;/*from  w  w  w.  j  a va  2  s  .  co  m*/
    while (m.find(i)) {
        System.out.print(m.group() + " ");
        i++;
    }
}

From source file:Main.java

public static void main(String[] args) {
    Pattern pattern = Pattern.compile("<Mark([^>]*)>");

    String myString = "this is a test <Mark this is a test> " + "<i>a</i> <Mark t> <b>x+y</b> <Mark 123>";
    Matcher matcher = pattern.matcher(myString);

    while (matcher.find()) {
        System.out.println(matcher.group(1));
    }//w  w  w . j a v a2  s .  c  o m
}

From source file:MainClass.java

public static void main(String args[]) {
    Pattern p = Pattern.compile("\\d");
    Matcher m1 = p.matcher("01234");

    while (m1.find()) {
        System.out.println("\t\t" + m1.group());
    }/*from   w ww.  j  a  v  a  2  s.co m*/
    m1.reset();
    System.out.println("After resetting the Matcher");
    while (m1.find()) {
        System.out.println("\t\t" + m1.group());
    }
}

From source file:MainClass.java

public static void main(String args[]) {
    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();/* ww w. j a  v a  2  s  .  c  om*/
    Pattern p2 = Pattern.compile(tmp);
    matcher = p.matcher("5");
    isOk = matcher.matches();
    System.out.println("second pattern matches " + isOk);
}

From source file:MainClass.java

public static void main(String args[]) {
    Pattern p = Pattern.compile("\\w(\\d)");
    String candidate = "A6 is Audi";

    Matcher matcher = p.matcher(candidate);
    if (matcher.find()) {
        String tmp = matcher.group(0);
        System.out.println(tmp);//from ww w . ja  va 2  s.c  o m

        tmp = matcher.group(1);
        System.out.println(tmp);
    }
}

From source file:MainClass.java

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

    String candidateString = "Java Java Java.";

    // Attempt to match the candidate String.
    Matcher matcher = p.matcher(candidateString);

    // loop though and display all matches
    while (matcher.find()) {
        System.out.println(matcher.group());
    }/*from  w ww .  j a v a2s.  c o  m*/
}

From source file:MainClass.java

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

    String candidateString = "My name is Bond. James Bond.";
    String matchHelper[] = { "               ^", "              ^", "                           ^",
            "                          ^" };
    Matcher matcher = p.matcher(candidateString);
    // Find the end point of the first 'B(ond)'
    matcher.find();//from  www. j  ava2 s.com
    int endIndex = matcher.end(0);
    System.out.println(candidateString);
    System.out.println(matchHelper[0] + endIndex);

}

From source file:MainClass.java

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

    String candidateString = "My name is Bond. James Bond.";
    String matchHelper[] = { "               ^", "              ^", "                           ^",
            "                          ^" };
    Matcher matcher = p.matcher(candidateString);
    matcher.find();/*w  w w .jav  a 2 s  .  c  om*/
    //  find the end point of the first sub group (ond)
    int nextIndex = matcher.end(1);
    System.out.println(candidateString);
    System.out.println(matchHelper[1] + nextIndex);

}

From source file:MainClass.java

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

    String candidateString = "My name is Bond. James Bond.";
    String matchHelper[] = { "               ^", "              ^", "                           ^",
            "                          ^" };
    Matcher matcher = p.matcher(candidateString);

    // find the end point of the second sub group (ond)
    int nextIndex = matcher.end(1);
    System.out.println(candidateString);
    System.out.println(matchHelper[3] + nextIndex);

}

From source file:MainClass.java

public static void main(String args[]) {
    Pattern p = Pattern.compile("(\\w+) (\\w+)");
    StringBuffer sb = new StringBuffer();

    String candidateString = "Jack Lee";

    String replacement = "$2, $1";
    Matcher matcher = p.matcher(candidateString);
    matcher.matches();/* www.  ja v a 2 s . c  om*/

    matcher.appendReplacement(sb, replacement);

    System.out.println(sb.toString());
}