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[] args) {
    CharSequence inputStr = "quit";
    String patternStr = "q";
    String replacementStr = "s";

    Pattern pattern = Pattern.compile(patternStr);

    Matcher matcher = pattern.matcher(inputStr);
    String output = matcher.replaceAll(replacementStr);
}

From source file:MainClass.java

public static void main(String[] av) {
    String joke = "dog " + "dogs";
    String regEx = "dog";

    Pattern doggone = Pattern.compile(regEx);
    Matcher m = doggone.matcher(joke);

    StringBuffer newJoke = new StringBuffer();
    while (m.find()) {
        m.appendReplacement(newJoke, "goat");
    }//from   ww w  .j  ava2s . c o m
    m.appendTail(newJoke);
    System.out.println(newJoke.toString());
}

From source file:MatcherEndExample.java

public static void main(String args[]) {
    String candidateString = "My name is Bond. James Bond.";
    String matchHelper[] = { "               ^", "                           ^" };
    Pattern p = Pattern.compile("Bond");
    Matcher matcher = p.matcher(candidateString);

    matcher.find();/*  ww w . ja  v a  2 s.  c  o m*/
    int endIndex = matcher.end();
    System.out.println(candidateString);
    System.out.println(matchHelper[0] + endIndex);

    matcher.find();
    int nextIndex = matcher.end();
    System.out.println(candidateString);
    System.out.println(matchHelper[1] + nextIndex);
}

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  w w .java  2s  . c o  m*/
    m.appendTail(result);
    System.out.println(result);
}

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();// ww  w  .j  av  a2 s .com
    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:TestRegularExpression.java

public static void main(String[] args) {
    if (args.length < 2) {
        System.out.println("Usage:\n" + "java TestRegularExpression " + "characterSequence regularExpression+");
        System.exit(0);/*from   w w  w  .  j  a  v a  2s. c om*/
    }
    System.out.println("Input: \"" + args[0] + "\"");
    for (int i = 1; i < args.length; i++) {
        System.out.println("Regular expression: \"" + args[i] + "\"");
        Pattern p = Pattern.compile(args[i]);
        Matcher m = p.matcher(args[0]);
        while (m.find()) {
            System.out.println("Match \"" + m.group() + "\" at positions " + m.start() + "-" + (m.end() - 1));
        }
    }
}

From source file:MainClass.java

public static void main(String args[]) {
    String str = "Java1 Java2 JDK Java2S Java2s.com";

    Pattern pat = Pattern.compile("Java.*? ");
    Matcher mat = pat.matcher(str);

    System.out.println("Original sequence: " + str);

    str = mat.replaceAll("Java ");

    System.out.println("Modified sequence: " + str);

}

From source file:FindA.java

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

    String candidate = "A Matcher examines the results of applying a pattern.";

    String regex = "\\ba\\w*\\b";
    Pattern p = Pattern.compile(regex);

    Matcher m = p.matcher(candidate);
    String val = null;
    System.out.println("INPUT: " + candidate);

    System.out.println("REGEX: " + regex + "\r\n");

    while (m.find()) {
        val = m.group();
        System.out.println("MATCH: " + val);
    }//  www . j a v  a 2 s .c  o  m
    if (val == null) {
        System.out.println("NO MATCHES: ");
    }
}

From source file:Main.java

public static void main(String args[]) {
    Pattern pat = Pattern.compile("\\b\\w+@XYZ\\.com\\b");

    Matcher mat = pat.matcher("t@XYZ.com\n" + "a@XYZ.com\n" + "n@XYZ.com");

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

From source file:Main.java

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

    String candidate = "this is a test, A TEST.";

    String regex = "\\ba\\w*\\b";
    Pattern p = Pattern.compile(regex);

    Matcher m = p.matcher(candidate);
    String val = null;
    System.out.println("INPUT: " + candidate);

    System.out.println("REGEX: " + regex + "\r\n");

    while (m.find()) {
        val = m.group();
        System.out.println("MATCH: " + val);
    }//from  ww w .ja v  a  2 s  .  c  o m

    if (val == null) {
        System.out.println("NO MATCHES: ");
    }
}