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 testString = " weight 600 KG AND HEIGHT 84900 CM";
    Pattern pattern = Pattern.compile("(?<=HEIGHT)\\s*([0-9]+)\\s*(?=CM)");
    Matcher matcher = pattern.matcher(testString);
    while (matcher.find()) {
        System.out.println("Height: '" + matcher.group(1) + "'");
    }/*ww  w  .ja  v a 2s .  com*/
}

From source file:Main.java

public static void main(String args[]) {
    Pattern p = Pattern.compile("test");
    StringBuffer sb = new StringBuffer();

    String candidateString = "This is a test.";

    String replacement = "Test";
    Matcher matcher = p.matcher(candidateString);
    matcher.find();

    matcher.appendReplacement(sb, replacement);
}

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
    }//from ww  w.j  a  v a2 s  .c  o m
}

From source file:MainClass.java

public static void main(String args[]) {
    Pattern p = Pattern.compile("\\w(\\d)");
    String candidate = "A6 is Audi";

    Matcher matcher = p.matcher(candidate);
    if (matcher.find()) {
        String tmp = matcher.group(0);
        System.out.println(tmp);/*from   w  w w . j a v a 2s. co  m*/

        tmp = matcher.group(1);
        System.out.println(tmp);
    }
}

From source file:Main.java

public static void main(String args[]) {
    Pattern p = Pattern.compile("\\w(\\d)");
    String candidate = "w5 ";

    Matcher matcher = p.matcher(candidate);
    if (matcher.find()) {
        String tmp = matcher.group(0);
        System.out.println(tmp);//w ww.  j a  v a  2 s .  c  om

        tmp = matcher.group(1);
        System.out.println(tmp);
    }
}

From source file:SimpleSubGroupExample.java

public static void main(String args[]) {
    Pattern p = Pattern.compile("\\w(\\d)");
    String candidate = "J9 is my favorite";

    Matcher matcher = p.matcher(candidate);
    if (matcher.find()) {
        String tmp = matcher.group(0);
        System.out.println(tmp);/*from   w  ww .  java2  s . c om*/

        tmp = matcher.group(1);
        System.out.println(tmp);
    }
}

From source file:Main.java

public static void main(String args[]) {

    // String to be scanned to find the pattern.
    String line = "This order was places.";
    String pattern = "(.*)(\\d+)(.*)";

    // Create a Pattern object
    Pattern r = Pattern.compile(pattern);

    // Now create matcher object.
    Matcher m = r.matcher(line);
    if (m.find()) {
        System.out.println("Found value: " + m.group(0));
        System.out.println("Found value: " + m.group(1));
        System.out.println("Found value: " + m.group(2));
    } else {// ww w  . j  a v a2s.co m
        System.out.println("NO MATCH");
    }
}

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 w  ww.  j  a va2s .  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 a  va 2 s .c  o  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:RegExpr5.java

public static void main(String args[]) {
    Pattern pat = Pattern.compile("e.+d");
    Matcher mat = pat.matcher("extend cup end table");

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