Example usage for java.util.regex Pattern matcher

List of usage examples for java.util.regex Pattern matcher

Introduction

In this page you can find the example usage for java.util.regex Pattern matcher.

Prototype

public Matcher matcher(CharSequence input) 

Source Link

Document

Creates a matcher that will match the given input against this pattern.

Usage

From source file:ReplaceExample.java

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

    String candidate = "X99SuperJava";
    Matcher matcher = pattern.matcher(candidate);
    String tmp = matcher.replaceAll("$33");

    System.out.println("REPLACEMENT: " + tmp);
    System.out.println("ORIGINAL: " + candidate);
}

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. ja va 2  s .c o  m
}

From source file:Main.java

public static void main(String args[]) {

    // String to be scanned to find the pattern.
    String line = "This order was places.";
    String pattern = "(.*)(\\d+)(.*)";

    // Create a Pattern object
    Pattern r = Pattern.compile(pattern);

    // Now create matcher object.
    Matcher m = r.matcher(line);
    if (m.find()) {
        System.out.println("Found value: " + m.group(0));
        System.out.println("Found value: " + m.group(1));
        System.out.println("Found value: " + m.group(2));
    } else {/*from   w  w w  . j a  va 2  s  . co m*/
        System.out.println("NO MATCH");
    }
}

From source file:MainClass.java

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

    Matcher matcher = p.matcher(candidate);
    if (matcher.find()) {
        String tmp = matcher.group(0);
        System.out.println(tmp);/*  w  w w . j a v  a  2  s .com*/

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

From source file:MainClass.java

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

    Matcher matcher = p.matcher(candidate);

    if (matcher.find()) {
        String tmp = matcher.group(0);
        System.out.println(tmp);//www .j  a  v  a 2 s . c o  m
    }
}

From source file:Grep0.java

public static void main(String[] args) throws IOException {
    BufferedReader is = new BufferedReader(new InputStreamReader(System.in));
    if (args.length != 1) {
        System.err.println("Usage: MatchLines pattern");
        System.exit(1);/*from   ww  w. j  ava  2  s  . c  o  m*/
    }
    Pattern patt = Pattern.compile(args[0]);
    Matcher matcher = patt.matcher("");
    String line = null;
    while ((line = is.readLine()) != null) {
        matcher.reset(line);
        if (matcher.find()) {
            System.out.println("MATCH: " + line);
        }
    }
}

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 w w . j  a va 2  s . c om*/
    }
}

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  ww w.  j  av  a2 s .c om*/
    }
}

From source file:Main.java

public static void main(String args[]) {
    String regex = "(\\w++)(\\d\\d)(\\w+)";
    Pattern pattern = Pattern.compile(regex);
    String candidate = "X99 test 55";
    Matcher matcher = pattern.matcher(candidate);

    if (matcher.find()) {
        System.out.println("GROUP 0:" + matcher.group(0));
        System.out.println("GROUP 1:" + matcher.group(1));
        System.out.println("GROUP 2:" + matcher.group(2));
        System.out.println("GROUP 3:" + matcher.group(3));
    } else {//from w  w  w .  j av a2  s . c  om
        System.out.println("NO MATCHES");
    }
}

From source file:MainClass.java

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

    String candidateString = "Ice. ice Ice Ice.";

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

    System.out.println(tmp);/*from w  ww.  j  a  v a2 s  . c  o m*/
}