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

public static void main(String[] argv) throws Exception {

    CharSequence inputStr = "Abc";
    String patternStr = "abc";

    // Compile with case-insensitivity
    Pattern pattern = Pattern.compile(patternStr, Pattern.CASE_INSENSITIVE);
    Matcher matcher = pattern.matcher(inputStr);
    boolean matchFound = matcher.matches();

    matchFound = pattern.matches("((?i)a)bc", "aBc");
    matchFound = pattern.matches("(?i:a)bc", "aBc");
    matchFound = pattern.matches("a((?i)b)c", "aBc");
    matchFound = pattern.matches("a(?i:b)c", "aBc");
}

From source file:Main.java

public static void main(String[] arguments) throws Exception {
    StringBuffer output = new StringBuffer();

    FileReader file = new FileReader("a.htm");
    BufferedReader buff = new BufferedReader(file);
    boolean eof = false;
    while (!eof) {
        String line = buff.readLine();
        if (line == null)
            eof = true;/*from  w ww  .  j a v a  2 s.c o  m*/
        else
            output.append(line + "\n");
    }
    buff.close();

    String page = output.toString();
    Pattern pattern = Pattern.compile("<a.+href=\"(.+?)\"");
    Matcher matcher = pattern.matcher(page);
    while (matcher.find()) {
        System.out.println(matcher.group(1));
    }
}

From source file:MainClass.java

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

    Pattern pattern = Pattern.compile(regex);

    String candidate = "W3C";

    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[]) {

    // String to be scanned to find the pattern.
    String line = "this is a test";
    String pattern = "is";

    // 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 the value");
    } else {//  ww  w  . ja  v  a 2s .c o m
        System.out.println("NO MATCH");
    }
}

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());
    }/*from w w  w .  j a  v  a 2s.  c o  m*/
}

From source file:ReluctantExample.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());
    }// ww  w  .  jav  a2s  . c  o m
    System.out.println("Done");
}

From source file:Main.java

License:asdf

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

    Matcher matcher = pattern.matcher(candidate);
    matcher.find();//from  w  w w  .  j a v a 2 s  . 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 regex = "[a-z]@.";
    Pattern p = Pattern.compile(regex);
    String str = "abc@yahoo.com,123@cnn.com,abc@google.com";
    Matcher m = p.matcher(str);
}

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());
    }/*  w ww . java2 s .  c  o m*/

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

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);/*from   w w w  . j  a  v a  2  s. co m*/
}