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[] argv) throws Exception {

    CharSequence inputStr = "a\r\rb"; // Mac
    //inputStr = "a\r\n\r\nb"; // Windows
    //inputStr = "a\n\nb"; // Unix

    String patternStr = "(^.*\\S+.*$)+";
    Pattern pattern = Pattern.compile(patternStr, Pattern.MULTILINE);
    Matcher matcher = pattern.matcher(inputStr);

    while (matcher.find()) {
        String paragraph = matcher.group();
    }//from   ww  w  . j ava2  s . c om
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    CharSequence inputStr = "a\rb";
    inputStr = "a\r\nb";
    inputStr = "a\nb";

    String patternStr = "^(.*)$";
    Pattern pattern = Pattern.compile(patternStr, Pattern.MULTILINE);
    Matcher matcher = pattern.matcher(inputStr);
    while (matcher.find()) {
        String lineWithTerminator = matcher.group(0);

        String lineWithoutTerminator = matcher.group(1);
    }// w  w  w  .  j a va2 s.c  om
}

From source file:Main.java

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

    CharSequence inputStr = "abbabcd";
    String patternStr = "(a(b*))+(c*)";

    Pattern pattern = Pattern.compile(patternStr);
    Matcher matcher = pattern.matcher(inputStr);
    boolean matchFound = matcher.find();

    if (matchFound) {
        // Get all groups for this match
        for (int i = 0; i <= matcher.groupCount(); i++) {
            // Get the group's captured text
            String groupStr = matcher.group(i);

            // Get the group's indices
            int groupStart = matcher.start(i);
            int groupEnd = matcher.end(i);

            // groupStr is equivalent to
            inputStr.subSequence(groupStart, groupEnd);
        }/*from   w  ww. j av a  2 s.  c  om*/
    }
}

From source file:MainClass.java

public static void main(String[] args) {
    Pattern p = Pattern.compile("^java", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
    Matcher m = p.matcher("java has regex\nJava has regex\n" + "JAVA has pretty good regular expressions\n"
            + "Regular expressions are in Java");
    while (m.find())
        System.out.println(m.group());
}

From source file:RESimple.java

public static void main(String[] argv) {
    String pattern = "^Q[^u]\\d+\\.";
    String input = "QA777. is the next flight. It is on time.";

    Pattern p = Pattern.compile(pattern);

    boolean found = p.matcher(input).lookingAt();

    System.out.println("'" + pattern + "'" + (found ? " matches '" : " doesn't match '") + input + "'");
}

From source file:Main.java

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

    CharSequence inputStr = "Abc";
    String patternStr = "abc";

    // Compile with case-insensitivity
    Pattern pattern = Pattern.compile(patternStr, Pattern.CASE_INSENSITIVE);
    Matcher matcher = pattern.matcher(inputStr);
    boolean matchFound = matcher.matches();

    matchFound = pattern.matches("[a-c]+", "aBc");
    matchFound = pattern.matches("(?i)[a-c]+", "aBc");

}

From source file:Main.java

public static void main(String[] args) {
    Pattern p = Pattern.compile("<row><column>(.*)</column></row>", Pattern.DOTALL);

    Matcher matcher = p.matcher("<row><column>Header\n\n\ntext</column></row>");

    if (matcher.matches()) {
        System.out.println(matcher.group(1));
    }/*w  ww .  ja  va  2  s . c om*/
}

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);//from   w w w.j  av  a  2  s .c o m

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

From source file:MainClass.java

public static void main(String args[]) {
    String regex = "(\\d+?)";
    Pattern pattern = Pattern.compile(regex);

    String candidate = "1234";

    Matcher matcher = pattern.matcher(candidate);

    System.out.println(matcher.group());

}

From source file:Main.java

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

    CharSequence inputStr = "Abc";
    String patternStr = "abc";

    // Compile with case-insensitivity
    Pattern pattern = Pattern.compile(patternStr, Pattern.CASE_INSENSITIVE);
    Matcher matcher = pattern.matcher(inputStr);
    boolean matchFound = matcher.matches();

    // Use an inline modifier
    matchFound = pattern.matches("abc", "aBc");
    matchFound = pattern.matches("(?i)abc", "aBc");
    matchFound = pattern.matches("a(?i)bc", "aBc");
}