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

private static void match(String REGEX, String INPUT) {
    Pattern p = Pattern.compile(REGEX);
    Matcher m = p.matcher(INPUT);
    int count = 0;

    while (m.find()) {
        count++;/*  ww w  . j  av a2  s.  c om*/
        System.out.println("\nMatch number: " + count);
        System.out.println("start() : " + m.start());
        System.out.println("end()   : " + m.end());
        System.out.println("group() : " + m.group());
    }
}

From source file:MatcherResetExample.java

public static void test() {
    Pattern p = Pattern.compile("\\d");
    Matcher m1 = p.matcher("01234");

    while (m1.find()) {
        System.out.println("\t\t" + m1.group());
    }/*from w  w  w  .  j a v a2s .  co m*/
    m1.reset();
    System.out.println("After resetting the Matcher");
    while (m1.find()) {
        System.out.println("\t\t" + m1.group());
    }
}

From source file:Main.java

private static String runSubRegex(String regex, String tag) {
    Pattern p = Pattern.compile(regex);
    Matcher matcher = p.matcher(tag);
    if (matcher.find()) {
        return matcher.group(1);
    }//www  . j ava2 s  .  c  o  m
    return null;
}

From source file:PatternMethodExample.java

public static void reusePatternMethodExample() {
    Pattern p = Pattern.compile("\\d");
    Matcher matcher = p.matcher("5");
    boolean isOk = matcher.matches();
    System.out.println("original pattern matches " + isOk);

    String tmp = p.pattern();//from w ww .j a va  2  s  . c  o m
    Pattern p2 = Pattern.compile(tmp);
    matcher = p.matcher("5");
    isOk = matcher.matches();
    System.out.println("second pattern matches " + isOk);
}

From source file:Main.java

public static CharSequence removeDuplicateWhitespace(CharSequence inputStr) {
    String patternStr = "\\s+";
    String replaceStr = " ";
    Pattern pattern = Pattern.compile(patternStr);
    Matcher matcher = pattern.matcher(inputStr);
    return matcher.replaceAll(replaceStr);
}

From source file:MatcherResetCharSequenceExample.java

public static void test() {
    String output = "";
    Pattern p = Pattern.compile("\\d");
    Matcher m1 = p.matcher("01234");

    while (m1.find()) {
        System.out.println("\t\t" + m1.group());
    }//from  w  ww  . j  a v a 2 s .  c  o m
    //now reset the matcher with new data
    m1.reset("56789");
    System.out.println("After resetting the Matcher");
    //iterate through the matcher
    while (m1.find()) {
        System.out.println("\t\t" + m1.group());
    }
}

From source file:Main.java

public static String find(String patternStr, CharSequence input) {
    Pattern pattern = Pattern.compile(patternStr);
    Matcher matcher = pattern.matcher(input);
    if (matcher.find()) {
        return matcher.group();
    }// w w w.j a  v  a 2s .  c  o m
    return null;
}

From source file:Main.java

public static String escapeRE(String str) {
    Pattern escaper = Pattern.compile("([^a-zA-z0-9])");
    return escaper.matcher(str).replaceAll("\\\\$1");
}

From source file:Main.java

public static int getNumber(String s1) throws Exception {
    Pattern pattern = Pattern.compile("([\\-0-9]*)[,]*.*");
    Matcher m = pattern.matcher(s1);
    if (m.find()) {
        return Integer.parseInt(m.group(1));
    }/*from www  .  j a v  a  2s.  c o  m*/

    throw new Exception("Not valid input");
}

From source file:Main.java

public static StringBuffer removePatternContents(String defStr, Pattern pat) {
    Matcher mat = pat.matcher(defStr);
    StringBuffer sb = new StringBuffer();
    while (mat.find()) {
        mat.appendReplacement(sb, " ");
    }//from ww w .j  av  a2s  .  c  om
    mat.appendTail(sb);
    return sb;
}