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:MainClass.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);

    // Find the starting point of the first 'Bond'
    matcher.find();/*w w w  .  j  a  v  a  2 s . c  o m*/
    int startIndex = matcher.start();
    System.out.println(candidateString);
    System.out.println(matchHelper[0] + startIndex);

}

From source file:Main.java

public static void main(String args[]) {
    // match a single digit
    Pattern p = Pattern.compile("\\d");
    Matcher matcher = p.matcher("5");
    boolean isOk = matcher.matches();
    System.out.println("original pattern matches " + isOk);

    // recycle the pattern
    String tmp = p.pattern();/*from w w w.  j a  v a2  s .co  m*/
    Pattern p2 = Pattern.compile(tmp);
    matcher = p.matcher("5");
    isOk = matcher.matches();
    System.out.println("second pattern matches " + isOk);
}

From source file:MainClass.java

public static void main(String[] args) {
    Pattern patt = Pattern.compile("(\\w+)\\s(\\d+)");
    Matcher matcher = patt.matcher("Bananas 123");
    matcher.lookingAt();/*w ww  . ja  v a 2 s .co m*/
    System.out.println("Name: " + matcher.group(1));
    System.out.println("Number: " + matcher.group(2));
}

From source file:Main.java

public static void main(String[] args) {
    String source = "The quick brown fox jumps over the brown lazy dog.";
    String find = "brown";
    String replace = "red";

    Pattern pattern = Pattern.compile(find);
    Matcher matcher = pattern.matcher(source);
    String output = matcher.replaceAll(replace);

    System.out.println("Source = " + source);
    System.out.println("Output = " + output);
}

From source file:Main.java

public static void main(String[] args) {
    String regex = "\\b(?<areaCode>\\d{3})(?<prefix>\\d{3})(?<postPhoneNumber>\\d{4})\\b";
    String source = "1234567890, 12345, and 9876543210";
    Pattern p = Pattern.compile(regex);

    Matcher m = p.matcher(source);
    while (m.find()) {
        String matchedText = m.group();
        int start1 = m.start("areaCode");
        int start2 = m.start("prefix");
        int start3 = m.start("postPhoneNumber");
        System.out.println("Matched Text:" + matchedText);
        System.out.println("Area code start:" + start1);
        System.out.println("Prefix start:" + start2);
        System.out.println("Line Number start:" + start3);
    }//from   ww  w.  j a  va2 s  . co  m
}

From source file:MainClass.java

public static void main(String[] args) {
    if (args.length < 2) {
        System.out.println("Usage:\n" + "java MainClass " + "characterSequence regularExpression+");
        System.exit(0);/*  w w w.  j av a  2s . co m*/
    }
    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 joke = "dog day daughter daut did do done date";
    String regEx = "d";

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

    StringBuffer newJoke = new StringBuffer();
    while (m.find())
        m.appendReplacement(newJoke, "g");
    m.appendTail(newJoke);//from   w  ww  .ja  v  a2 s  .c  om
    System.out.println(newJoke.toString());
}

From source file:Main.java

public static void main(String[] args) {
    String s = "bla bla {a.b.c} bla bla bla {0,1,2} bla bla";
    Pattern p = Pattern.compile("\\{([^\\{\\}]*)\\}");

    Matcher m = p.matcher(s);

    while (m.find()) {
        System.out.println(m.group(1));
    }//from  ww  w. j a  v a  2s  .c  o m
}

From source file:Main.java

public static void main(String[] args) {
    CharSequence inputStr = "q w e r t ";
    String patternStr = "q";
    String replacementStr = "s";
    Pattern pattern = Pattern.compile(patternStr);

    Matcher matcher = pattern.matcher(inputStr);
    String output = matcher.replaceAll(replacementStr);
    System.out.println(output);//from   w  w w .  j ava  2s . c  o m
}

From source file:Main.java

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

    Pattern pattern = Pattern.compile("pattern");
    Matcher matcher = pattern.matcher("infile.txt");

    // Find all matches
    while (matcher.find()) {
        // Get the matching string
        String match = matcher.group();
    }// w ww.j  a v a  2s .c  om
}