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 to be scanned to find the pattern.
    String line = "This order was places.";
    String pattern = "(.*)(\\d+)(.*)";

    // Create a Pattern object
    Pattern r = Pattern.compile(pattern);

    // Now create matcher object.
    Matcher m = r.matcher(line);/*w  ww.  j ava  2  s  . c  o  m*/
    if (m.find()) {
        System.out.println("Found value: " + m.group(0));
        System.out.println("Found value: " + m.group(1));
        System.out.println("Found value: " + m.group(2));
    } else {
        System.out.println("NO MATCH");
    }
}

From source file:Main.java

public static void main(String[] args) {

    String s = "java2s.com 1 + 1 = 2.0";

    Scanner scanner = new Scanner(s);

    // find a pattern of 2 letters before com, with horizon of 5
    System.out.println(scanner.findWithinHorizon(Pattern.compile("..com"), 5));

    // find a pattern of 2 letters before com, with horizon of 10
    System.out.println(scanner.findWithinHorizon(Pattern.compile("..com"), 10));

    // print the rest of the string
    System.out.println(scanner.nextLine());

    scanner.close();/* ww w . jav a 2 s.  c  om*/
}

From source file:MainClass.java

public static void main(String args[]) {
    String str = "Java1 Java2 JDK Java2S Java2s.com";

    Pattern pat = Pattern.compile("Java.*? ");
    Matcher mat = pat.matcher(str);

    System.out.println("Original sequence: " + str);

    str = mat.replaceAll("Java ");

    System.out.println("Modified sequence: " + str);

}

From source file:Main.java

public static void main(String[] args) {

    String s = "java2s.com 1 + 1 = 2.0 true ";

    Scanner scanner = new Scanner(s);

    System.out.println(scanner.nextLine());

    // change the delimiter of this scanner
    scanner.useDelimiter(Pattern.compile(".ll."));

    // display the new delimiter
    System.out.println(scanner.delimiter());

    scanner.close();//from ww  w.j a v a2 s. co  m
}

From source file:MainClass.java

public static void main(String args[]) {

    String phrase = "a word word";
    String duplicatePattern = "\\b(\\w+) \\1\\b";

    Pattern p = null;//w w w .  j a  v a  2 s  .  c  o m
    try {
        p = Pattern.compile(duplicatePattern);
    } catch (PatternSyntaxException pex) {
        pex.printStackTrace();
        System.exit(0);
    }
    // count the number of matches.
    int matches = 0;

    // get the matcher
    Matcher m = p.matcher(phrase);
    String val = null;

    // find all matching Strings
    while (m.find()) {
        val = ":" + m.group() + ":";
        System.out.println(val);
        matches++;
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    // Use COMMENTS but include a character class with a space

    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 w  w. ja v  a  2  s. c o  m*/

    patternStr = "a  [\\ ]  b";
    pattern = Pattern.compile(patternStr, Pattern.COMMENTS);
    matcher = pattern.matcher(inputStr);
    matchFound = matcher.matches();
}

From source file:NonCapturingGroupExample.java

public static void main(String args[]) {
    String regex = "hello|hi|greetings|(?:good morning)";

    String candidate1 = "Java2s say hi to you";
    String candidate2 = "Java2s say good morning to you";
    Pattern pattern = Pattern.compile(regex);

    Matcher matcher = pattern.matcher(candidate1);
    System.out.println("GROUP COUNT:" + matcher.groupCount());

    if (matcher.find())
        System.out.println("GOT 1:" + candidate1);

    matcher.reset();/*w  w  w  .  j  a  v  a  2  s  .c  o  m*/
    matcher = pattern.matcher(candidate2);

    System.out.println("GROUP COUNT:" + matcher.groupCount());

    if (matcher.find())
        System.out.println("GOT 2:" + candidate2);
}

From source file:SplitDemo.java

public static void main(String[] args) {
    String input = "This!!unusual use!!of exclamation!!points";
    System.out.println(Arrays.asList(Pattern.compile("!!").split(input)));
    // Only do the first three:
    System.out.println(Arrays.asList(Pattern.compile("!!").split(input, 3)));
    System.out.println(Arrays.asList("Aha! String has a split() built in!".split(" ")));
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String filename = "infile.txt";
    String patternStr = "pattern";
    BufferedReader rd = new BufferedReader(new FileReader(filename));

    Pattern pattern = Pattern.compile(patternStr);
    Matcher matcher = pattern.matcher("\\D");

    String line = null;/*w w w  .ja  v a  2  s. com*/
    while ((line = rd.readLine()) != null) {
        matcher.reset(line);
        if (matcher.find()) {
            // line matches the pattern
        }
    }
}

From source file:Main.java

public static void main(String[] args) {
    Pattern pattern = null;/*  w  ww. j a va2 s.  c om*/
    Matcher matcher = null;

    Console console = System.console();
    while (true) {
        try {
            pattern = Pattern.compile(console.readLine("%nEnter your regex: "));

            matcher = pattern.matcher(console.readLine("Enter input string to search: "));
        } catch (PatternSyntaxException pse) {
            console.format("There is a problem with the regular expression!%n");
            console.format("The pattern in question is: %s%n", pse.getPattern());
            console.format("The description is: %s%n", pse.getDescription());
            console.format("The message is: %s%n", pse.getMessage());
            console.format("The index is: %s%n", pse.getIndex());
            System.exit(0);
        }
        boolean found = false;
        while (matcher.find()) {
            console.format("I found the text \"%s\" starting at " + "index %d and ending at index %d.%n",
                    matcher.group(), matcher.start(), matcher.end());
            found = true;
        }
        if (!found) {
            console.format("No match found.%n");
        }
    }
}