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 pat = Pattern.compile("X+");
    Matcher mat = pat.matcher("X XX XXX");

    while (mat.find())
        System.out.println("Match: " + mat.group());
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    String regex = "(?<=http://)\\S+";
    Pattern pattern = Pattern.compile(regex);
    String candidate = "http://www.a.com.";
    Matcher matcher = pattern.matcher(candidate);
    while (matcher.find()) {
        String msg = ":" + matcher.group() + ":";
        System.out.println(msg);/*from  w w w .  j  a  va  2s.c o  m*/
    }
}

From source file:Main.java

public static void main(String[] args) {
    String testString = " weight 600 KG AND HEIGHT 84900 CM";
    Pattern pattern = Pattern.compile("(?<=HEIGHT)\\s*([0-9]+)\\s*(?=CM)");
    Matcher matcher = pattern.matcher(testString);
    while (matcher.find()) {
        System.out.println("Height: '" + matcher.group(1) + "'");
    }//from w ww  .j a va2  s  . c o m
}

From source file:Main.java

public static void main(String[] args) {
    String s = "bla bla {a.b.c} bla bla bla {0,1,2} bla bla";
    Pattern p = Pattern.compile("\\{([^\\{\\}]*)\\}");

    Matcher m = p.matcher(s);//from w ww  . ja  v  a2 s  . c  o  m

    while (m.find()) {
        System.out.println(m.group(1));
    }
}

From source file:MainClass.java

public static void main(String args[]) {
    String regex = "(\\w+)(\\d\\d)(\\w+)";

    Pattern pattern = Pattern.compile(regex);

    String candidate = "AAA99SuperJava";

    Matcher matcher = pattern.matcher(candidate);

    matcher.find();//from   w  ww  .ja v  a  2s .co  m

    System.out.println(matcher.group(1));
    System.out.println(matcher.group(2));
    System.out.println(matcher.group(3));
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String input = "123 321 443 52134 432";
    Pattern pattern = Pattern.compile("\\d+");
    Matcher matcher = pattern.matcher(input);
    int i = 0;/*from  w w  w. j  a  v a 2 s . c om*/
    while (matcher.find()) {
        ++i;
    }
    System.out.printf("Matched %d times%n", i);
}

From source file:Main.java

public static void main(String[] asd) {
    String sourcestring = "source string to match with pattern";
    Pattern re = Pattern.compile("[^ABC\\s+]");
    Matcher m = re.matcher(sourcestring);
    int mIdx = 0;
    while (m.find()) {
        for (int groupIdx = 0; groupIdx < m.groupCount() + 1; groupIdx++) {
            System.out.println("[" + mIdx + "][" + groupIdx + "] = " + m.group(groupIdx));
        }/* w  w w .j a  va 2 s  .c  o m*/
        mIdx++;
    }
}

From source file:GreedyExample.java

public static void main(String args[]) {
    String regex = "(\\w+)(\\d\\d)(\\w+)";

    Pattern pattern = Pattern.compile(regex);

    String candidate = "X99SuperJava";

    Matcher matcher = pattern.matcher(candidate);

    matcher.find();/*w w w.j a v a 2s .c o  m*/

    System.out.println(matcher.group(1));
    System.out.println(matcher.group(2));
    System.out.println(matcher.group(3));
}

From source file:Main.java

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

    matcher.find();/*from ww  w . j  a  v a 2  s. c o m*/
    int endIndex = matcher.end();
    System.out.println(candidateString);
    System.out.println(endIndex);

}

From source file:Main.java

public static void main(String args[]) throws Exception {
    String duplicatePattern = "\\b(\\w+) \\1\\b";
    Pattern p = Pattern.compile(duplicatePattern);

    int matches = 0;
    String phrase = "this is a test";
    Matcher m = p.matcher(phrase);
    String val = null;
    while (m.find()) {
        val = ":" + m.group() + ":";
        System.out.println(val);
        matches++;//from  w w  w .j  a v a 2  s .c o  m
    }
}