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 s = "java2s.com 1 + 1 = 2.0";

    Scanner scanner = new Scanner(s);

    // find a pattern of any letter plus "com"
    System.out.println(scanner.findInLine(Pattern.compile(".com")));

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

    scanner.close();/*w  w  w.  j  a va  2s .co  m*/
}

From source file:Main.java

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

    CharSequence inputStr = "abbabcd";
    String patternStr = "(a(b*))+(c*)";

    // Compile and use regular expression
    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++) {
            String groupStr = matcher.group(i);
        }/*w  w  w . ja  v a2 s.c om*/
    }
}

From source file:Main.java

public static void main(String[] args) {
    String regex = "\\b(\\d{3})(\\d{3})(\\d{4})\\b";
    String replacementText = "($1) $2-$3";
    String source = "1234567890, 12345, and 9876543210";

    Pattern p = Pattern.compile(regex);
    Matcher m = p.matcher(source);

    String formattedSource = m.replaceAll(replacementText);

    System.out.println("Text: " + source);
    System.out.println("Formatted Text: " + formattedSource);
}

From source file:MainClass.java

public static void main(String args[]) {
    String joke = "dog day daughter daut did do done date";
    String regEx = "d";

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

    StringBuffer newJoke = new StringBuffer();
    while (m.find())
        m.appendReplacement(newJoke, "g");
    m.appendTail(newJoke);/* w w  w  .ja va 2s .c o  m*/
    System.out.println(newJoke.toString());
}

From source file:Main.java

public static void main(String args[]) {

    // String to be scanned to find the pattern.
    String line = "this is a test";
    String pattern = "is";

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

    // Now create matcher object.
    Matcher m = r.matcher(line);//w ww.j a va  2 s .  co  m
    if (m.find()) {
        System.out.println("Found the value");
    } else {
        System.out.println("NO MATCH");
    }
}

From source file:MatcherStartExample.java

public static void main(String args[]) {
    String candidateString = "My name is Bond. James Bond.";
    String matchHelper[] = { "          ^", "                      ^" };
    Pattern p = Pattern.compile("Bond");
    Matcher matcher = p.matcher(candidateString);

    //Find the starting point of the first 'Bond'
    matcher.find();/*from  www.j a  v a 2 s  .  c om*/
    int startIndex = matcher.start();
    System.out.println(candidateString);
    System.out.println(matchHelper[0] + startIndex);

    //Find the starting point of the second 'Bond'
    matcher.find();
    int nextIndex = matcher.start();
    System.out.println(candidateString);
    System.out.println(matchHelper[1] + nextIndex);

}

From source file:MatcherEndExample.java

public static void main(String args[]) {
    String candidateString = "My name is Bond. James Bond.";
    String matchHelper[] = { "               ^", "                           ^" };
    Pattern p = Pattern.compile("Bond");
    Matcher matcher = p.matcher(candidateString);

    matcher.find();//w  w  w .  ja va  2s.c om
    int endIndex = matcher.end();
    System.out.println(candidateString);
    System.out.println(matchHelper[0] + endIndex);

    matcher.find();
    int nextIndex = matcher.end();
    System.out.println(candidateString);
    System.out.println(matchHelper[1] + nextIndex);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Pattern pattern = Pattern.compile("pattern");
    FileInputStream input = new FileInputStream("file.txt");
    FileChannel channel = input.getChannel();

    ByteBuffer bbuf = channel.map(FileChannel.MapMode.READ_ONLY, 0, (int) channel.size());
    CharBuffer cbuf = Charset.forName("8859_1").newDecoder().decode(bbuf);

    Matcher matcher = pattern.matcher(cbuf);
    while (matcher.find()) {
        String match = matcher.group();
        System.out.println(match);
    }/* w ww.ja v  a2s  . co  m*/
}

From source file:MainClass.java

public static void main(String[] args) {
    String[] input = new String[] { "Java has regular expressions in 1.4",
            "regular expressions now expressing in Java", "Java represses oracular expressions" };

    Pattern p1 = Pattern.compile("re\\w*"), p2 = Pattern.compile("Java.*");

    for (int i = 0; i < input.length; i++) {
        System.out.println("input " + i + ": " + input[i]);
        Matcher m1 = p1.matcher(input[i]), m2 = p2.matcher(input[i]);
        while (m1.find())
            System.out.println("m1.find() '" + m1.group() + "' start = " + m1.start() + " end = " + m1.end());
    }//from  w  w  w.  jav a 2s  .  c  o  m
}

From source file:MainClass.java

public static void main(String[] args) {
    String[] input = new String[] { "Java has regular expressions in 1.4",
            "regular expressions now expressing in Java", "Java represses oracular expressions" };

    Pattern p1 = Pattern.compile("re\\w*"), p2 = Pattern.compile("Java.*");

    for (int i = 0; i < input.length; i++) {
        System.out.println("input " + i + ": " + input[i]);
        Matcher m1 = p1.matcher(input[i]), m2 = p2.matcher(input[i]);
        if (m1.lookingAt()) // No reset() necessary
            System.out.println("m1.lookingAt() start = " + m1.start() + " end = " + m1.end());

    }//from ww w.j  a  v  a  2  s.  c o  m
}