Example usage for java.util.regex Pattern matches

List of usage examples for java.util.regex Pattern matches

Introduction

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

Prototype

public static boolean matches(String regex, CharSequence input) 

Source Link

Document

Compiles the given regular expression and attempts to match the given input against it.

Usage

From source file:Main.java

public static void main(String args[]) {

    String regex = "ad*";
    String input = "add";

    boolean isMatch = Pattern.matches(regex, input);
    System.out.println(isMatch);//  w ww .j av  a  2  s .com
}

From source file:Main.java

public static void main(String args[]) {

    String regex = "ad*";
    String input = "add";

    boolean isMatch = Pattern.matches(regex, input);
    System.out.println(isMatch);// return true
}

From source file:Main.java

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

    CharSequence inputStr = "abc\ndef";
    String patternStr = ".*c.+d.*";

    Pattern pattern = Pattern.compile(patternStr, Pattern.DOTALL);
    Matcher matcher = pattern.matcher(inputStr);
    boolean matchFound = matcher.matches();

    matchFound = pattern.matches(".*c.+d.*", "abc\r\ndef");
    matchFound = pattern.matches("(?s).*c.+d.*", "abc\r\ndef");
}

From source file:PatternMatchesTest.java

public static void main(String args[]) {

    String regex = "ad*";
    String input = "add";

    boolean isMatch = Pattern.matches(regex, input);
    System.out.println(isMatch);//return true
}

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[] 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("((?i)a)bc", "aBc");
    matchFound = pattern.matches("(?i:a)bc", "aBc");
    matchFound = pattern.matches("a((?i)b)c", "aBc");
    matchFound = pattern.matches("a(?i:b)c", "aBc");
}

From source file:Main.java

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

    String patternStr = "i.e.";

    boolean matchFound = Pattern.matches(patternStr, "i.e.");
    matchFound = Pattern.matches(patternStr, "ibex");

    // Quote the pattern; i.e. surround with \Q and \E
    matchFound = Pattern.matches("\\Q" + patternStr + "\\E", "i.e.");
    matchFound = Pattern.matches("\\Q" + patternStr + "\\E", "ibex");

    // Escape the pattern
    patternStr = escapeRE(patternStr);/*from w w  w .j  a  va2  s  . co  m*/

    matchFound = Pattern.matches(patternStr, "i.e.");
    matchFound = Pattern.matches(patternStr, "ibex");

    // Returns a pattern where all punctuation characters are escaped.

}

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");
}

From source file:Main.java

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

    CharSequence inputStr = "abc\ndef";
    String patternStr = "abc$";

    // Compile with multiline enabled
    Pattern pattern = Pattern.compile(patternStr, Pattern.MULTILINE);
    Matcher matcher = pattern.matcher(inputStr);
    boolean matchFound = matcher.find(); // true

    // Use an inline modifier to enable multiline mode
    matchFound = pattern.matches(".*abc$.*", "abc\r\ndef"); // false
    matchFound = pattern.matches("(?m).*abc$.*", "abc\r\ndef"); // true
}

From source file:Main.java

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

    CharSequence inputStr = "a b";
    String patternStr = "a b";

    // Compile without comments
    Pattern pattern = Pattern.compile(patternStr);
    Matcher matcher = pattern.matcher(inputStr);
    boolean matchFound = matcher.matches();

    // Compile with comments
    pattern = Pattern.compile(patternStr, Pattern.COMMENTS);
    matcher = pattern.matcher(inputStr);
    matchFound = matcher.matches();/*from  w ww.  j  ava2 s.com*/

    matchFound = pattern.matches("(?x)a \t\n \\s b", inputStr);

}