Example usage for java.util.regex Matcher find

List of usage examples for java.util.regex Matcher find

Introduction

In this page you can find the example usage for java.util.regex Matcher find.

Prototype

public boolean find() 

Source Link

Document

Attempts to find the next subsequence of the input sequence that matches the pattern.

Usage

From source file:Main.java

public static void main(String[] args) {
    String str = "this is a test";

    StringBuffer sb = new StringBuffer();
    Matcher m = Pattern.compile("([a-z])([a-z]*)", Pattern.CASE_INSENSITIVE).matcher(str);
    while (m.find()) {
        m.appendReplacement(sb, m.group(1).toUpperCase() + m.group(2).toLowerCase());
    }//from  w  ww.ja  v  a 2s.c o  m
    System.out.println(m.appendTail(sb).toString());
}

From source file:Main.java

public static void main(String[] args) { // main method
    Pattern pattern = Pattern.compile("(?<=sentence).*");
    Matcher matcher = pattern.matcher("this is a test!");
    boolean found = false;
    while (matcher.find()) { // if it is found
        System.out.println("I found the text: " + matcher.group().toString());
        found = true;/* w w w .  j  a v  a 2s.  c o  m*/
    }
    if (!found) { // if not
        System.out.println("I didn't find the text."); // say it wasn't found
    }
}

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***");
    }//ww  w . j  a  v  a2s. c o m
    m.appendTail(result);
    System.out.println(result);
}

From source file:FindDemo.java

public static void main(String[] args) {
    Matcher m = Pattern.compile("\\w+").matcher("Evening is full of the linnet's wings");
    while (m.find())
        System.out.println(m.group());
    int i = 0;/*w  w  w .  j av a 2  s .c om*/
    while (m.find(i)) {
        System.out.print(m.group() + " ");
        i++;
    }

}

From source file:StartEnd.java

public static void main(String[] args) {
    String[] input = new String[] { "Java has regular expressions in 1.4",
            "regular expressions now expressing in Java", "Java represses oracular expressions" };
    Pattern p1 = Pattern.compile("re\\w*"), p2 = Pattern.compile("Java.*");
    for (int i = 0; i < input.length; i++) {
        System.out.println("input " + i + ": " + input[i]);
        Matcher m1 = p1.matcher(input[i]), m2 = p2.matcher(input[i]);
        while (m1.find())
            System.out.println("m1.find() '" + m1.group() + "' start = " + m1.start() + " end = " + m1.end());
        while (m2.find())
            System.out.println("m2.find() '" + m2.group() + "' start = " + m2.start() + " end = " + m2.end());
        if (m1.lookingAt()) // No reset() necessary
            System.out.println("m1.lookingAt() start = " + m1.start() + " end = " + m1.end());
        if (m2.lookingAt())
            System.out.println("m2.lookingAt() start = " + m2.start() + " end = " + m2.end());
        if (m1.matches()) // No reset() necessary
            System.out.println("m1.matches() start = " + m1.start() + " end = " + m1.end());
        if (m2.matches())
            System.out.println("m2.matches() start = " + m2.start() + " end = " + m2.end());
    }/*from ww  w .j a va2 s.  c  o m*/
}

From source file:Main.java

public static void main(String args[]) {
    Pattern p = Pattern.compile("t(est)");
    String candidateString = "This is a test. This is another test.";
    Matcher matcher = p.matcher(candidateString);
    // Find group number 0 of the first find
    matcher.find();
    String group_0 = matcher.group(0);
    String group_1 = matcher.group(1);
    System.out.println("Group 0 " + group_0);
    System.out.println("Group 1 " + group_1);
    System.out.println(candidateString);

}

From source file:PositiveLookaheadExample.java

public static void main(String args[]) {
    String regex = "(?=^255).*";

    Pattern pattern = Pattern.compile(regex);

    String candidate = "255.0.0.1";

    Matcher matcher = pattern.matcher(candidate);

    String ip = "not found";

    if (matcher.find())
        ip = matcher.group();// w w w .j a v a2s.c o  m

    String msg = "ip: " + ip;

    System.out.println(msg);
}

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 w  w .j a  va  2 s .co  m
    System.out.println(newJoke.toString());
}

From source file:MainClass.java

public static void main(String args[]) {
    Pattern p = Pattern.compile("B(on)d");

    String candidateString = "My name is Bond. James Bond.";
    String matchHelper[] = { "               ^", "              ^", "                           ^",
            "                          ^" };
    Matcher matcher = p.matcher(candidateString);
    // Find the end point of the first 'B(ond)'
    matcher.find();
    int endIndex = matcher.end(0);
    System.out.println(candidateString);
    System.out.println(matchHelper[0] + endIndex);

}

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 w  w w.java 2  s. com
    m.appendTail(newJoke);
    System.out.println(newJoke.toString());
}