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[]) {
    String output = "";
    Pattern p = Pattern.compile("\\d");
    Matcher m1 = p.matcher("01234");

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

From source file:MatcherGroupParamExample.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);

    matcher.find();//  ww w .jav  a2  s . c  o m
    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);
    System.out.println(candidateString);

    matcher.find();
    group_0 = matcher.group(0);
    group_1 = matcher.group(1);
    System.out.println("Group 0 " + group_0);
    System.out.println("Group 1 " + group_1);
    System.out.println(candidateString);
}

From source file:Main.java

License:asdf

public static void main(String args[]) {
    String regex = "\\w+";
    Pattern pattern = Pattern.compile(regex);

    String candidate = "asdf Java2s.com";

    Matcher matcher = pattern.matcher(candidate);

    if (matcher.find()) {
        System.out.println("GROUP 0:" + matcher.group(0));
    }/*from  w ww  .  j  a  v  a 2 s  .c om*/

}

From source file:MatcherLookingAtExample.java

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

    String candidateString_1 = "J2SE is the only one for me";
    String candidateString_2 = "For me, it's J2SE, or nothing at all";
    String candidateString_3 = "J2SEistheonlyoneforme";

    Matcher matcher = p.matcher(candidateString_1);

    String msg = ":" + candidateString_1 + ": matches?: ";
    System.out.println(msg + matcher.lookingAt());
    matcher.reset(candidateString_2);/*from  w ww .  j  a v a 2  s.c om*/

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

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

    String candidateString = "My name is Bond. James Bond.";

    String replacement = "$1 Waldo $2";

    Matcher matcher = p.matcher(candidateString);
    matcher.find();/* ww w .ja v  a 2  s .  c o  m*/

    matcher.appendReplacement(sb, replacement);

    String msg = sb.toString();
    System.out.println(msg);
}

From source file:Main.java

public static void main(String args[]) {
    String regex = "(\\d+?)";
    Pattern pattern = Pattern.compile(regex);

    String candidate = "1234";

    Matcher matcher = pattern.matcher(candidate);

    while (matcher.find()) {
        System.out.println(matcher.group());
    }/*from w  w  w .  ja  va 2  s  .c  o m*/

    System.out.println("Done");
}

From source file:MainClass.java

public static void main(String args[]) {
    String regex = "(\\d+?)";
    Pattern pattern = Pattern.compile(regex);

    String candidate = "1234";

    Matcher matcher = pattern.matcher(candidate);

    System.out.println(matcher.group());

}

From source file:Main.java

public static void main(String[] args) {
    String s = new String("\"Hello\" hello");
    Pattern p = Pattern.compile("\"([^\"]*)\"");
    Matcher m = p.matcher(s);/*from w  w  w  .j a  v  a 2s  .  c o m*/
    while (m.find()) {
        System.out.println(m.group(1));
    }
}

From source file:MainClass.java

public static void main(String args[]) {
    Pattern pat = Pattern.compile("java");
    Matcher mat = pat.matcher("www.java2s.com 1 2 3 j a v a 2 s.c o m");

    while (mat.find()) {
        System.out.println("java found at index " + mat.start());
    }/*from   www  .j  a va  2s .  c  o m*/
}

From source file:MatcherStartParamExample.java

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

    String candidateString = "My name is Bond. James Bond.";
    String matchHelper[] = { "          ^", "           ^", "                      ^",
            "                       ^" };
    Matcher matcher = p.matcher(candidateString);
    matcher.find();/* w  w w .j  a  va2 s.  co  m*/
    int startIndex = matcher.start(0);
    System.out.println(candidateString);
    System.out.println(matchHelper[0] + startIndex);

    int nextIndex = matcher.start(1);
    System.out.println(candidateString);
    System.out.println(matchHelper[1] + nextIndex);

    matcher.find();
    startIndex = matcher.start(0);
    System.out.println(candidateString);
    System.out.println(matchHelper[2] + startIndex);

    nextIndex = matcher.start(1);
    System.out.println(candidateString);
    System.out.println(matchHelper[3] + nextIndex);

}