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) {
    String testString = " weight 600 KG AND HEIGHT 84900 CM";
    Pattern pattern = Pattern.compile("(?<=HEIGHT)\\s*([0-9]+)\\s*(?=CM)");
    Matcher matcher = pattern.matcher(testString);
    while (matcher.find()) {
        System.out.println("Height: '" + matcher.group(1) + "'");
    }/*from www .j  a v a 2  s .c o m*/
}

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  2s . c om*/

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

From source file:Main.java

public static void main(String args[]) {
    Pattern p = Pattern.compile("Java \\d");

    String candidate = "Java 4";
    Matcher m = p.matcher(candidate);

    if (m != null)
        System.out.println(m.find());
}

From source file:GetParen0.java

public static void main(String[] args) {
    Pattern myRE = Pattern.compile("d.*ian");
    Matcher matcher = myRE.matcher("darwinian pterodactyls soared over the devonian space");
    matcher.lookingAt();//from w  ww  . j a v a  2 s  . com
    String result = matcher.group(0);
    System.out.println(result);
}

From source file:Main.java

public static void main(String[] args) {

    String src = "abc :  def"; // two spaces after colon.
    String tgt = "ghi :   jkl"; // three spaces after colon.

    Pattern spaces = Pattern.compile("([^:]*:)(\\s*)(.*)");

    Matcher mSrc = spaces.matcher(src);
    Matcher mTgt = spaces.matcher(tgt);

    mSrc.matches();/*from www .j  av a2s  .c  om*/
    mTgt.matches();

    System.out.println("Spaces in src: " + mSrc.group(2).length());
    System.out.println("Spaces in tgt: " + mTgt.group(2).length());

    System.out.println("Target with src's number of spaces: " + mTgt.group(1) + mSrc.group(2) + mTgt.group(3));
}

From source file:Main.java

public static void main(String[] arguments) throws Exception {
    String page = loadPage(arguments[0]);
    Pattern pattern = Pattern.compile("<a.+href=\"(.+?)\"");
    Matcher matcher = pattern.matcher(page);
    while (matcher.find()) {
        System.out.println(matcher.group(1));
    }/*  w w  w.  j av a 2 s  .c  o m*/
}

From source file:Main.java

public static void main(String[] args) {
    String s = "this is a good example with 234 songs";

    Pattern p = Pattern.compile("this is a (.*?) example with (\\d+) songs");
    Matcher m = p.matcher(s);
    if (m.matches()) {
        String kind = m.group(1);
        String nbr = m.group(2);/*from   w  w w .j  a  va2s .c o m*/

        System.out.println("kind: " + kind + " nbr: " + nbr);
    }
}

From source file:RegexTest.java

public static void main(String[] args) {
    Pattern p = Pattern.compile(REGEX);
    Matcher m = p.matcher(INPUT); // get a matcher object
    StringBuffer sb = new StringBuffer();
    while (m.find()) {
        m.appendReplacement(sb, REPLACE);
    }//ww  w.j a  va 2s  .c o m
    m.appendTail(sb);
    System.out.println(sb.toString());
}

From source file:MatcherTest.java

public static void main(String[] argv) {
    Pattern p = Pattern.compile(REGEX);
    Matcher m = p.matcher(INPUT); // get a matcher object
    int count = 0;
    while (m.find()) {
        count++;//from w w w  .j av a 2 s .  c o m
        System.out.println("Match number " + count);
        System.out.println("start(): " + m.start());
        System.out.println("end(): " + m.end());
    }
}

From source file:Main.java

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

    CharSequence inputStr = "abc\ndef";
    String patternStr = "abc$";

    // Compile with multiline enabled
    Pattern pattern = Pattern.compile(patternStr, Pattern.MULTILINE);
    Matcher matcher = pattern.matcher(inputStr);
    boolean matchFound = matcher.find(); // true

    // Use an inline modifier to enable multiline mode
    matchFound = pattern.matches(".*abc$.*", "abc\r\ndef"); // false
    matchFound = pattern.matches("(?m).*abc$.*", "abc\r\ndef"); // true
}