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 void main(String[] argv) throws Exception {
    Pattern word = Pattern.compile("\\G\\w+");

    Matcher mat = word.matcher("this is a test 999");
    System.out.println(mat.find());

}

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 'B(ond)'
    matcher.find();/*from   ww w. j  av a2s  . co  m*/
    int endIndex = matcher.end(0);
    System.out.println(candidateString);
    System.out.println(matchHelper[2] + endIndex);

}

From source file:SimpleGroupExample.java

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

    Matcher matcher = p.matcher(candidate);

    if (matcher.find()) {
        String tmp = matcher.group(0);
        System.out.println(tmp);//from   w ww . jav a2  s  .  c om
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Pattern punct = Pattern.compile("\\G\\p{Punct}");

    Matcher mat = punct.matcher("this is a test 999");
    System.out.println(mat.find());

}

From source file:MatcherFindExample.java

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

    String candidateString = "I love Java2s. Java2s is about Java.";

    Matcher matcher = p.matcher(candidateString);

    while (matcher.find()) {
        System.out.println(matcher.group());
    }/*from w ww  .ja  v a2  s  .com*/

}

From source file:MatcherReplaceFirstExample.java

public static void main(String args[]) {
    Pattern p = Pattern.compile("(i|I)ce");
    String candidateString = "I love ice. Ice is my favorite. Ice Ice Ice.";

    Matcher matcher = p.matcher(candidateString);
    String tmp = matcher.replaceFirst("Java");
    System.out.println(tmp);/* w ww. j  a v a  2 s  .  c om*/
}

From source file:Main.java

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

    Pattern p = Pattern.compile("t(est)");
    String candidateString = "This is a test. This is a test.";
    Matcher matcher = p.matcher(candidateString);
    matcher.find();/*from  w  w  w.j  a  v  a 2  s .  c  om*/
    String group_0 = matcher.group(0);
    String group_1 = matcher.group(1);
    System.out.println("Group 0 " + group_0);
    System.out.println("Group 1 " + group_1);
}

From source file:MatcherGroupCountExample.java

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

    String candidateString = "My name is Bond. James Bond.";
    Matcher matcher = p.matcher(candidateString);

    int numberOfGroups = matcher.groupCount();
    System.out.println("numberOfGroups =" + numberOfGroups);

}

From source file:SimpleSubGroupExample.java

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

    Matcher matcher = p.matcher(candidate);
    if (matcher.find()) {
        String tmp = matcher.group(0);
        System.out.println(tmp);//  w ww  . j  a  va  2s . c om

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

From source file:Main.java

public static void main(String[] args) { // main method
    Pattern pattern = Pattern.compile("(?<=sentence).*");
    Matcher matcher = pattern.matcher("this is a test!");
    boolean found = false;
    while (matcher.find()) { // if it is found
        System.out.println("I found the text: " + matcher.group().toString());
        found = true;/*from ww  w  .ja v a 2  s .  c o  m*/
    }
    if (!found) { // if not
        System.out.println("I didn't find the text."); // say it wasn't found
    }
}