Example usage for java.util.regex Pattern matcher

List of usage examples for java.util.regex Pattern matcher

Introduction

In this page you can find the example usage for java.util.regex Pattern matcher.

Prototype

public Matcher matcher(CharSequence input) 

Source Link

Document

Creates a matcher that will match the given input against this pattern.

Usage

From source file:Main.java

public static void main(String[] args) {
    Pattern p = Pattern.compile(REGEX);
    //  get a matcher object
    Matcher m = p.matcher(INPUT);
    List<String> sequences = new Vector<String>();
    while (m.find()) {
        sequences.add(INPUT.substring(m.start(), m.end()));
    }/*from   ww w .  j  ava 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 + ">");
    }// w  w  w.java 2s  . c  o m
    matcher.appendTail(buf);

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

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("(?x)a \\s b", inputStr);

}

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 (?x:  b   )", inputStr);
}

From source file:RE_QnotU_Args.java

public static void main(String[] argv) {
    String patt = "^Q[^u]\\d+\\.";
    Pattern r = Pattern.compile(patt);
    Matcher m = r.matcher("RE_QnotU_Args");
    boolean found = m.lookingAt();
    System.out.println(patt + (found ? " matches " : " doesn't match ") + "RE_QnotU_Args");

}

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);

    while (m.find()) {
        matchedText = m.group();//from   w  w w. j  a  va 2  s .c o m
        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: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));
    }//w  w  w . java2  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 {

    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  w w.  ja  v a 2 s. c  om*/
    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:Main.java

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

    CharSequence inputStr = "abc\ndef";
    String patternStr = ".*c.+d.*";

    Pattern pattern = Pattern.compile(patternStr, Pattern.DOTALL);
    Matcher matcher = pattern.matcher(inputStr);
    boolean matchFound = matcher.matches();

    matchFound = pattern.matches(".*c.+d.*", "abc\r\ndef");
    matchFound = pattern.matches("(?s).*c.+d.*", "abc\r\ndef");
}

From source file:MatcherDemo.java

public static void main(String[] args) {
    Pattern p = Pattern.compile(REGEX);
    Matcher m = p.matcher(INPUT); // get a matcher object
    int count = 0;
    while (m.find()) {
        count++;//from  ww w . ja  v a2s.  com
        System.out.println("Match number " + count);
        System.out.println("start(): " + m.start());
        System.out.println("end(): " + m.end());
    }
}