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

public static void main(String args[]) {
    Pattern pat = Pattern.compile("test");
    Matcher mat = pat.matcher("test 1 2 3 test");

    while (mat.find()) {
        System.out.println("test found at index " + mat.start());
    }/*  w ww .  j  a  v  a  2s.  co m*/
}

From source file:Main.java

public static void main(String args[]) {
    Pattern p = Pattern.compile("Java");
    String candidateString = "Java Java Java.";
    Matcher matcher = p.matcher(candidateString);
    while (matcher.find()) {
        System.out.println(matcher.group());
    }// w ww  . ja v  a 2s. co m
}

From source file:Test.java

public static void main(String[] args) {
    Pattern p = Pattern.compile("([\\w\\s]+) word");
    Matcher m = p.matcher("Could you test a phrase with some word");
    while (m.find()) {
        System.err.println(m.group(1));
        System.err.println(m.group());
    }/*ww w . j av a2  s .com*/
}

From source file:Main.java

public static void main(String args[]) {
    Pattern p = Pattern.compile("t(est)");
    String candidateString = "This is a test. This is another test.";
    Matcher matcher = p.matcher(candidateString);

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

From source file:Main.java

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

    String candidate = "Java 4";
    Matcher m = p.matcher(candidate);

    if (m != null)
        System.out.println(m.find());
}

From source file:Main.java

public static void main(String args[]) {
    Pattern p = Pattern.compile("(i|I)ce");
    String candidateString = "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[] args) {
    Pattern p = Pattern.compile("\\d\\d?\\d?");
    Matcher m = p.matcher("test this 536 in it");
    while (m.find()) {
        System.out.println(m.group()); // print the age in your string
        System.out.println(m.start()); // print the position in the string where it starts
    }/*www.  j  a  va 2 s. co  m*/
}

From source file:Main.java

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

    Matcher matcher = p.matcher(candidateString);
    String tmp = matcher.replaceAll("Java");

    System.out.println(tmp);/*from w  w w. jav  a  2 s  . com*/
}

From source file:Main.java

public static void main(String args[]) {

    Pattern p = Pattern.compile("\\d");
    Matcher m1 = p.matcher("55");
    Matcher m2 = p.matcher("fdshfdgdfh");

    System.out.println(m1.pattern() == m2.pattern());
    // return true
}

From source file:Main.java

public static void main(String args[]) {
    Pattern p = Pattern.compile("t(est)");
    String candidateString = "This is a test. This is another test.";
    Matcher matcher = p.matcher(candidateString);

    int nextIndex = matcher.start(1);
    System.out.println(candidateString);
    System.out.println(nextIndex);

}