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

public static void main(String args[]) {
    Pattern pat = Pattern.compile("[a-z]+");
    Matcher mat = pat.matcher("this is a test.");

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

From source file:Main.java

public static void main(String[] args) {
    String str = "_6654_1";

    Pattern p = Pattern.compile("_(\\d+)_1");
    Matcher m = p.matcher(str);
    if (m.matches())
        System.out.println(m.group(1)); // prints 6654
}

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[] argv) throws Exception {
    Pattern word = Pattern.compile("\\G\\w+");

    Matcher mat = word.matcher("this is a test 999");
    System.out.println(mat.find());

}

From source file:Main.java

public static void main(String[] args) {
    Pattern p = Pattern.compile("\\d\\d?\\d?");
    Matcher m = p.matcher("test this 536 in it");
    while (m.find()) {
        System.out.println(m.group()); // print the age in your string
        System.out.println(m.start()); // print the position in the string where it starts
    }// w  w  w . j ava2s.  c  om
}

From source file:MainClass.java

public static void main(String[] args) {
    Pattern myRE = Pattern.compile("d.*ian");
    Matcher matcher = myRE.matcher("danian devonian dan");
    matcher.lookingAt();//from w  ww  .  jav  a  2  s  .c o  m
    String result = matcher.group(0);
    System.out.println(result);
}

From source file:Main.java

public static void main(String[] args) {
    String regex = "\\b(\\d{3})(\\d{3})(\\d{4})\\b";
    String replacementText = "($1) $2-$3";
    String source = "1234567890, 12345, and 9876543210";

    Pattern p = Pattern.compile(regex);
    Matcher m = p.matcher(source);

    String formattedSource = m.replaceAll(replacementText);

    System.out.println("Text: " + source);
    System.out.println("Formatted Text: " + formattedSource);
}

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   w  ww  .  j  av  a2s  . c o  m*/
}

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);
    while (m.find()) {
        System.out.println(m.group(1));
    }/* w  ww  .j a v a  2s  . c  om*/
}

From source file:MainClass.java

public static void main(String args[]) {
    Pattern p = Pattern.compile("\\d");
    Matcher m1 = p.matcher("01234");

    while (m1.find()) {
        System.out.println("\t\t" + m1.group());
    }/*from   www  .j  a  v  a  2  s .  c o  m*/
    m1.reset();
    System.out.println("After resetting the Matcher");
    while (m1.find()) {
        System.out.println("\t\t" + m1.group());
    }
}