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

public static void main(String args[]) {
    String regex = "(?=^255).*";

    Pattern pattern = Pattern.compile(regex);

    String candidate = "255.0.0.1";

    Matcher matcher = pattern.matcher(candidate);

    String ip = "not found";

    if (matcher.find())
        ip = matcher.group();/*from  w  ww  . j  av  a2  s.  c  o m*/

    String msg = "ip: " + ip;

    System.out.println(msg);
}

From source file:Main.java

public static void main(String[] args) {
    String input = " javaId=2 test test javaId=33432. javaId=100";

    Pattern p = Pattern.compile("(javaId=)(\\d+)");
    Matcher m = p.matcher(input);

    StringBuffer result = new StringBuffer();
    while (m.find()) {
        System.out.println("Masking: " + m.group(2));
        m.appendReplacement(result, m.group(1) + "***masked***");
    }//from  w  ww  . j  a  va2  s .co  m
    m.appendTail(result);
    System.out.println(result);
}

From source file:MainClass.java

public static void main(String[] args) {
    String input = "This!!unusual use!!of exclamation!!points";
    System.out.println(Arrays.asList(Pattern.compile("!!").split(input)));

    System.out.println(Arrays.asList(Pattern.compile("!!").split(input, 3)));
    System.out.println(Arrays.asList("Aha! String has a split() built in!".split(" ")));
}

From source file:MainClass.java

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

    System.out.println("Looking for Java in Java 2.");

    if (mat.find())
        System.out.println("subsequence found");
    else//from   w w  w . jav  a2  s . c  o m
        System.out.println("No Match");
}

From source file:RegExpr.java

public static void main(String args[]) {
    Pattern pat;/*from   w  w  w  .  j  a v  a  2  s  .  co m*/
    Matcher mat;
    boolean found;

    pat = Pattern.compile("Java");
    mat = pat.matcher("Java");

    found = mat.matches();

    if (found)
        System.out.println("Matches");
    else
        System.out.println("No Match");

    mat = pat.matcher("Java 2");

    found = mat.matches();

    if (found)
        System.out.println("Matches");
    else
        System.out.println("No Match");
}

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);

    // Find the starting point of the first 'test'
    matcher.find();//from  w ww  .  j  a v  a 2  s .  c  om
    int startIndex = matcher.start();
    System.out.println(candidateString);
    System.out.println(startIndex);

    // Find the starting point of the second 'test'
    matcher.find();
    int nextIndex = matcher.start();
    System.out.println(candidateString);
    System.out.println(nextIndex);
}

From source file:Main.java

public static void main(String[] args) {
    String pattern = "[,\\s]+";
    String colours = "Red,White, Blue   Green        Yellow, Orange";

    Pattern splitter = Pattern.compile(pattern);
    String[] result = splitter.split(colours);

    for (String colour : result) {
        System.out.println("Colour = \"" + colour + "\"");
    }/*from  w ww  .  j ava2s . co m*/
}

From source file:Main.java

public static void main(String[] args) {
    String hello = "HelloxxxHelloxxxHello"; // String you want to 'examine'
    Pattern pattern = Pattern.compile("Hello"); // Pattern string you want to be
                                                // matched
    Matcher matcher = pattern.matcher(hello);

    int count = 0;
    while (matcher.find()) {
        count++; // count any matched pattern
    }//from ww  w .j  ava2s. co  m
    System.out.println(count);
}

From source file:Main.java

public static void main(String[] args) {
    String str = "This is a 23.4.123 test.";
    String[] s = str.split(" ");
    Pattern p = Pattern.compile("(\\d)+\\.(\\d)+");
    double d;// w w  w .  j av a2 s .co m
    for (int i = 0; i < s.length; i++) {
        Matcher m = p.matcher(s[i]);
        while (m.find()) {
            d = Double.parseDouble(m.group());
            System.out.println(d);
        }
    }
}

From source file:MainClass.java

public static void main(String args[]) {
    String statement = "a b c d e f g h i j k l";

    String splitPattern = "e|c|a|a|(a b d e)|(b c e)";

    Pattern p = Pattern.compile(splitPattern);

    String[] tokens = p.split(statement);

    for (int i = 0; i < tokens.length; i++) {
        System.out.println(tokens[i]);
    }//w  w w  .  j  a  v  a2s  .  co m
}