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

public static void main(String args[]) throws Exception {
    String regex = "(?<=http://)\\S+";
    Pattern pattern = Pattern.compile(regex);

    String candidate = "The Java2s website can be found at ";
    candidate += "http://www.java2s.com. There, ";
    candidate += "you can find information about some of ";
    candidate += "best example codes";

    Matcher matcher = pattern.matcher(candidate);
    while (matcher.find()) {
        String msg = ":" + matcher.group() + ":";
        System.out.println(msg);/*www.  ja  va 2  s  .  c o  m*/
    }
}

From source file:PositiveLookBehindExample.java

public static void main(String args[]) throws Exception {
    String regex = "(?<=http://)\\S+";

    Pattern pattern = Pattern.compile(regex);

    String candidate = "The Java2s website can be found at ";
    candidate += "http://www.java2s.com. There, ";
    candidate += "you can find some Java examples.";

    Matcher matcher = pattern.matcher(candidate);

    while (matcher.find()) {
        String msg = ":" + matcher.group() + ":";
        System.out.println(msg);/*from  w  ww .j a v a  2  s  .  com*/
    }
}

From source file:PositiveLookBehindExample.java

public static void main(String args[]) throws Exception {
    String regex = "(?<=http://)\\S+";

    Pattern pattern = Pattern.compile(regex);

    String candidate = "The Java2s website can be found at ";
    candidate += "http://www.java2s.com. There, ";
    candidate += "you can find some best example code for Java.";

    Matcher matcher = pattern.matcher(candidate);

    while (matcher.find()) {
        String msg = ":" + matcher.group() + ":";
        System.out.println(msg);//from  w w  w  .  j  a  v  a2  s  .c  o  m
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String patternStr = "\\((\\w+)\\)";
    String replaceStr = "<$1>";
    Pattern pattern = Pattern.compile(patternStr);

    CharSequence inputStr = "a (b c) d (ef) g";
    Matcher matcher = pattern.matcher(inputStr);
    String output = matcher.replaceAll(replaceStr);

}

From source file:Main.java

public static void main(String[] args) {
    String s = "stuff1 (foo1(bar1)foo2) stuff2 (bar2) stuff3";
    String re = "\\([^()]*\\)";
    Pattern p = Pattern.compile(re);
    Matcher m = p.matcher(s);/*from  w  ww  .  j a  v a 2 s  .  c o m*/
    while (m.find()) {
        s = m.replaceAll("");
        m = p.matcher(s);
    }
    System.out.println(s);
}

From source file:Main.java

public static void main(final String[] args) {
    final String[] lines = new String[] { "1. New", "2. UK", "3. France" };

    final Pattern p = Pattern.compile("\\.\\s+(.*?)\\s+-\\s+(.*)");

    for (final String unparsedText : lines) {
        String newspaper;/*from   w w  w .  j a v  a  2 s . c o m*/
        String country;

        final Matcher m = p.matcher(unparsedText);

        if (m.find()) {
            newspaper = m.group(1);
            country = m.group(2);

            System.out.println("Newspaper: " + newspaper + " Country: " + country);
        }
    }
}

From source file:MainClass.java

public static void main(String[] av) {
    String joke = "dog " + "dogs";
    String regEx = "dog";

    Pattern doggone = Pattern.compile(regEx);
    Matcher m = doggone.matcher(joke);

    StringBuffer newJoke = new StringBuffer();
    while (m.find()) {
        m.appendReplacement(newJoke, "goat");
    }/*from w w w  .  ja v  a2s . c o  m*/
    m.appendTail(newJoke);
    System.out.println(newJoke.toString());
}

From source file:Main.java

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

    // Compile regular expression
    String patternStr = "b";
    Pattern pattern = Pattern.compile(patternStr);

    // Determine if there is an exact match
    CharSequence inputStr = "a b c";
    Matcher matcher = pattern.matcher(inputStr);
    boolean matchFound = matcher.matches();

    // Try a different input
    matcher.reset("b");
    matchFound = matcher.matches();/*w w  w  .  j  a v  a 2  s.c om*/

    // Determine if pattern matches beginning of input
    matchFound = matcher.lookingAt();

}

From source file:MainClass.java

public static void main(String args[]) {
    // Match lowercase words.
    Pattern pat = Pattern.compile("[a-z]+");
    Matcher mat = pat.matcher("www.java2s.com.");

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

From source file:Main.java

public static void main(String[] args) {
    String s = "(123, 234; 345, 456) (567, 788; 899, 900)";
    Matcher m = Pattern.compile("\\d+").matcher(s);
    List<Integer> numbers = new ArrayList<Integer>();
    while (m.find()) {
        numbers.add(Integer.parseInt(m.group()));
    }/*w  w w.  j  a  v  a2s  .  c  o m*/
    System.out.println(numbers);
}