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) 

Source Link

Document

Compiles the given regular expression into a pattern.

Usage

From source file:Main.java

public static void main(String[] args) {
    String string = "var1[value1], var2[value2], var3[value3]";
    Pattern pattern = Pattern.compile("(\\[)(.*?)(\\])");
    Matcher matcher = pattern.matcher(string);

    List<String> listMatches = new ArrayList<String>();

    while (matcher.find()) {
        listMatches.add(matcher.group(2));
    }/*from   w  ww.  ja v  a  2 s.  c o  m*/

    for (String s : listMatches) {
        System.out.println(s);
    }
}

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 w  w .j a va  2s.  c  o  m*/
    }
}

From source file:Main.java

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

    CharSequence inputStr = "ab12 cd efg34 123";
    String patternStr = "([a-zA-Z]+[0-9]+)";

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

    StringBuffer buf = new StringBuffer();
    boolean found = false;
    while ((found = matcher.find())) {
        String replaceStr = matcher.group();
        matcher.appendReplacement(buf, "found<" + replaceStr + ">");
    }//from  w ww .j av  a 2 s.  c  om
    matcher.appendTail(buf);

    String result = buf.toString();
    System.out.println(result);
}

From source file:Main.java

public static void main(String[] arguments) throws Exception {
    String page = loadPage(arguments[0]);
    Pattern pattern = Pattern.compile("<a.+href=\"(.+?)\"");
    Matcher matcher = pattern.matcher(page);
    while (matcher.find()) {
        System.out.println(matcher.group(1));
    }//from   ww w.j a  va  2s  .  c om
}

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  www  . j av a2 s .c om

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

}

From source file:Main.java

public static void main(String[] args) {
    String source = "The quick brown fox jumps over the brown lazy dog.";
    String find = "brown";
    String replace = "red";

    Pattern pattern = Pattern.compile(find);
    Matcher matcher = pattern.matcher(source);
    String output = matcher.replaceAll(replace);

    System.out.println("Source = " + source);
    System.out.println("Output = " + output);
}

From source file:Main.java

public static void main(String[] args) {

    String src = "abc :  def"; // two spaces after colon.
    String tgt = "ghi :   jkl"; // three spaces after colon.

    Pattern spaces = Pattern.compile("([^:]*:)(\\s*)(.*)");

    Matcher mSrc = spaces.matcher(src);
    Matcher mTgt = spaces.matcher(tgt);

    mSrc.matches();//ww  w.jav a 2 s  .  com
    mTgt.matches();

    System.out.println("Spaces in src: " + mSrc.group(2).length());
    System.out.println("Spaces in tgt: " + mTgt.group(2).length());

    System.out.println("Target with src's number of spaces: " + mTgt.group(1) + mSrc.group(2) + mTgt.group(3));
}

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;//from   ww  w .j  a v  a  2 s.  c om
    while (m.find(i)) {
        System.out.print(m.group() + " ");
        i++;
    }

}

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(); // false

    // Use an inline modifier
    matchFound = pattern.matches("a b", inputStr);
    matchFound = pattern.matches("(?x)a b", inputStr);
}

From source file:MainClass.java

public static void main(String[] args) {
    Matcher m = Pattern.compile("(?m)(\\S+)\\s+((\\S+)\\s+(\\S+))$").matcher(poem);
    while (m.find()) {
        for (int j = 0; j <= m.groupCount(); j++)
            System.out.print("[" + m.group(j) + "]");
        System.out.println();/*w w w  .  ja v a 2 s  . c o m*/
    }
}