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

public static void main(final String[] args) {
    File path = new File(".");
    String[] list;/*from w  ww  . ja v a2  s  .  co  m*/
    if (args.length == 0)
        list = path.list();
    else
        list = path.list(new FilenameFilter() {
            private Pattern pattern = Pattern.compile(args[0]);

            public boolean accept(File dir, String name) {
                return pattern.matcher(new File(name).getName()).matches();
            }
        });
    Arrays.sort(list);
    for (int i = 0; i < list.length; i++)
        System.out.println(list[i]);
}

From source file:Main.java

public static void main(String[] args) {
    String regex = "\\b\\d+\\b";
    StringBuffer sb = new StringBuffer();
    String replacementText = "";
    String matchedText = "";

    String text = "We have 7 tutorials for Java, 2 tutorials for Javascript and 1 tutorial for Oracle.";

    Pattern p = Pattern.compile(regex);
    Matcher m = p.matcher(text);//w w w  . ja v a 2 s .  com

    while (m.find()) {
        matchedText = m.group();
        int num = Integer.parseInt(matchedText);
        if (num == 1) {
            replacementText = "only one";
        } else if (num < 5) {
            replacementText = "a few";
        } else {
            replacementText = "many";
        }
        m.appendReplacement(sb, replacementText);
    }

    m.appendTail(sb);
    System.out.println("Old  Text: " + text);
    System.out.println("New Text: " + sb.toString());
}

From source file:RegExpExample.java

public static void main(String args[]) {
    String fileName = "RETestSource.java";

    String unadornedClassRE = "^\\s*class (\\w+)";
    String doubleIdentifierRE = "\\b(\\w+)\\s+\\1\\b";

    Pattern classPattern = Pattern.compile(unadornedClassRE);
    Pattern doublePattern = Pattern.compile(doubleIdentifierRE);
    Matcher classMatcher, doubleMatcher;

    int lineNumber = 0;

    try {// www  .j a v a 2 s  .  com
        BufferedReader br = new BufferedReader(new FileReader(fileName));
        String line;

        while ((line = br.readLine()) != null) {
            lineNumber++;

            classMatcher = classPattern.matcher(line);
            doubleMatcher = doublePattern.matcher(line);

            if (classMatcher.find()) {
                System.out.println("The class [" + classMatcher.group(1) + "] is not public");
            }

            while (doubleMatcher.find()) {
                System.out.println("The word \"" + doubleMatcher.group(1) + "\" occurs twice at position "
                        + doubleMatcher.start() + " on line " + lineNumber);
            }
        }
    } catch (IOException ioe) {
        System.out.println("IOException: " + ioe);
        ioe.printStackTrace();
    }
}

From source file:MainClass.java

public static void main(String[] av) {
    String regEx = "[+|-]?(\\d+(\\.\\d*)?)|(\\.\\d+)";
    String str = "a b c d e 1 2 3 4.5 ";
    Pattern pattern = Pattern.compile(regEx);
    Matcher m = pattern.matcher(str);
    while (m.find()) {
        for (int i = 0; i <= m.groupCount(); i++) {
            System.out.println("Group " + i + ": " + m.group(i)); // Group i substring
        }//w  w  w .  j  a va 2s.co  m
    }
}

From source file:StartEnd.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());
        while (m2.find())
            System.out.println("m2.find() '" + m2.group() + "' start = " + m2.start() + " end = " + m2.end());
        if (m1.lookingAt()) // No reset() necessary
            System.out.println("m1.lookingAt() start = " + m1.start() + " end = " + m1.end());
        if (m2.lookingAt())
            System.out.println("m2.lookingAt() start = " + m2.start() + " end = " + m2.end());
        if (m1.matches()) // No reset() necessary
            System.out.println("m1.matches() start = " + m1.start() + " end = " + m1.end());
        if (m2.matches())
            System.out.println("m2.matches() start = " + m2.start() + " end = " + m2.end());
    }//from   w  w  w.ja v a 2 s .  c o  m
}

From source file:MainClass.java

public static void main(String[] args) {
    if (args.length < 2) {
        System.out.println("Usage:\n" + "java MainClass " + "characterSequence regularExpression+");
        System.exit(0);/* ww w .j  a  v  a2s  .  c  o m*/
    }
    System.out.println("Input: \"" + args[0] + "\"");
    for (int i = 1; i < args.length; i++) {
        System.out.println("Regular expression: \"" + args[i] + "\"");
        Pattern p = Pattern.compile(args[i]);
        Matcher m = p.matcher(args[0]);
        while (m.find()) {
            System.out.println("Match \"" + m.group() + "\" at positions " + m.start() + "-" + (m.end() - 1));
        }
    }
}

From source file:Grep.java

public static void main(String args[]) throws Exception {
    String regex = "";
    InputStream in = System.in;

    regex = args[0];//from   w w  w .j  a v a  2s .com
    in = new BufferedInputStream(new FileInputStream(args[1]));
    Pattern p = null;

    p = Pattern.compile(regex);

    BufferedReader buff = new BufferedReader(new InputStreamReader(in));

    String a;
    for (a = buff.readLine(); a != null; a = buff.readLine()) {
        Matcher m = p.matcher(a);
        if (m.find()) {
            System.out.println(a);
        }
    }
}

From source file:PatternSplit.java

public static void main(String args[]) {

    String statement = "I will not compromise. I will not "
            + "cooperate. There will be no concession, no conciliation, no "
            + "finding the middle ground, and no give and take.";

    String tokens[] = null;//from w  w  w . j  ava 2 s . c o m
    String splitPattern = "compromise|cooperate|concession|"
            + "conciliation|(finding the middle ground)|(give and take)";

    Pattern p = Pattern.compile(splitPattern);

    tokens = p.split(statement);

    System.out.println("REGEX PATTERN:\n" + splitPattern + "\n");

    System.out.println("STATEMENT:\n" + statement + "\n");

    System.out.println("TOKENS:");
    for (int i = 0; i < tokens.length; i++) {
        System.out.println(tokens[i]);
    }
}

From source file:Main.java

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

    URL url = new URL("http://www.java.com/");
    URLConnection urlConnection = url.openConnection();
    DataInputStream dis = new DataInputStream(urlConnection.getInputStream());
    String html = "", tmp = "";
    while ((tmp = dis.readUTF()) != null) {
        html += " " + tmp;
    }//from w ww  . jav  a  2 s.com
    dis.close();

    html = html.replaceAll("\\s+", " ");
    Pattern p = Pattern.compile("<title>(.*?)</title>");
    Matcher m = p.matcher(html);
    while (m.find() == true) {
        System.out.println(m.group(1));
    }
}

From source file:Groups.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  ww. j  a v a 2  s.c o m
    }

}