Example usage for java.util.regex Pattern compile

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

Introduction

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

Prototype

public static Pattern compile(String regex, int flags) 

Source Link

Document

Compiles the given regular expression into a pattern with the given flags.

Usage

From source file:Main.java

public static void main(String args[]) {
    Pattern p = Pattern.compile("test", Pattern.CASE_INSENSITIVE);
    String candidateString = "Test.";
    Matcher matcher = p.matcher(candidateString);
    matcher.find(0);/* ww w.j av  a2  s. co  m*/
    System.out.println(matcher.group());

}

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));
    }/*from w w w  . ja  v  a  2s . co  m*/
}

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:Main.java

public static void main(String args[]) {
    Pattern p = Pattern.compile("java", Pattern.CASE_INSENSITIVE);

    String candidateString = "Java. java JAVA jAVA";

    Matcher matcher = p.matcher(candidateString);

    // display the latter match
    System.out.println(candidateString);
    matcher.find(11);//  w w w.  j  a  v  a  2 s  . c  om
    System.out.println(matcher.group());

    // display the earlier match
    System.out.println(candidateString);
    matcher.find(0);
    System.out.println(matcher.group());
}

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  w w.  ja  v  a 2  s  . co m*/
    System.out.println(m.appendTail(sb).toString());
}

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: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();

    // 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";
    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 {

    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
}