Example usage for java.util.regex Matcher matches

List of usage examples for java.util.regex Matcher matches

Introduction

In this page you can find the example usage for java.util.regex Matcher matches.

Prototype

public boolean matches() 

Source Link

Document

Attempts to match the entire region against the pattern.

Usage

From source file:Main.java

public static void main(String[] args) {
    Pattern p = Pattern.compile("<row><column>(.*)</column></row>", Pattern.DOTALL);

    Matcher matcher = p.matcher("<row><column>Header\n\n\ntext</column></row>");

    if (matcher.matches()) {
        System.out.println(matcher.group(1));
    }/*from   w w w  .j  a v a  2  s .  c o  m*/
}

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 b", inputStr);
    matchFound = pattern.matches("(?x)a b", inputStr);
}

From source file:Main.java

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

    // Compile regular expression
    String patternStr = "b";
    Pattern pattern = Pattern.compile(patternStr);

    // Determine if there is an exact match
    CharSequence inputStr = "a b c";
    Matcher matcher = pattern.matcher(inputStr);
    boolean matchFound = matcher.matches();

    // Try a different input
    matcher.reset("b");
    matchFound = matcher.matches();//  w  w  w. jav  a  2 s  .  c  om

    // Determine if pattern matches beginning of input
    matchFound = matcher.lookingAt();

}

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: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  ww.  j a  va  2  s  . 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) {

    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();
    mTgt.matches();/*  w  w w .  j a  v a2 s. c  o  m*/

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

public static void main(String argv[]) {

    String logEntryPattern = "^([\\d.]+) (\\S+) (\\S+) \\[([\\w:/]+\\s[+\\-]\\d{4})\\] \"(.+?)\" (\\d{3}) (\\d+) \"([^\"]+)\" \"([^\"]+)\"";

    System.out.println("Using RE Pattern:");
    System.out.println(logEntryPattern);

    System.out.println("Input line is:");
    System.out.println(logEntryLine);

    Pattern p = Pattern.compile(logEntryPattern);
    Matcher matcher = p.matcher(logEntryLine);
    if (!matcher.matches() || NUM_FIELDS != matcher.groupCount()) {
        System.err.println("Bad log entry (or problem with RE?):");
        System.err.println(logEntryLine);
        return;/*from   w  ww . j ava 2  s  . c  o  m*/
    }
    System.out.println("IP Address: " + matcher.group(1));
    System.out.println("Date&Time: " + matcher.group(4));
    System.out.println("Request: " + matcher.group(5));
    System.out.println("Response: " + matcher.group(6));
    System.out.println("Bytes Sent: " + matcher.group(7));
    if (!matcher.group(8).equals("-"))
        System.out.println("Referer: " + matcher.group(8));
    System.out.println("Browser: " + matcher.group(9));
}

From source file:RegExTest.java

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.println("Enter pattern: ");
    String patternString = in.nextLine();

    Pattern pattern = null;/*from   ww  w . j a v a2  s .c o m*/
    try {
        pattern = Pattern.compile(patternString);
    } catch (PatternSyntaxException e) {
        System.out.println("Pattern syntax error");
        System.exit(1);
    }

    while (true) {
        System.out.println("Enter string to match: ");
        String input = in.nextLine();
        if (input == null || input.equals(""))
            return;
        Matcher matcher = pattern.matcher(input);
        if (matcher.matches()) {
            System.out.println("Match");
            int g = matcher.groupCount();
            if (g > 0) {
                for (int i = 0; i < input.length(); i++) {
                    for (int j = 1; j <= g; j++)
                        if (i == matcher.start(j))
                            System.out.print('(');
                    System.out.print(input.charAt(i));
                    for (int j = 1; j <= g; j++)
                        if (i + 1 == matcher.end(j))
                            System.out.print(')');
                }
                System.out.println();
            }
        } else
            System.out.println("No match");
    }
}

From source file:com.rslakra.java.TestUrlConnection.java

public static void main(String[] args) {
    String urlString = "https://devamatre.com/";
    HttpResponse httpResponse = HTTPHelper.executeGetRequest(urlString, null, true);
    System.out.println(httpResponse.getRequestHeaders());

    String formActionValue = extractFormActionValue(httpResponse.getDataBytes());
    System.out.println("\nformActionValue:\n" + formActionValue);

    String dataString = new String(httpResponse.getDataBytes());
    Pattern pattern = Pattern.compile("\"");
    Matcher matcher = pattern.matcher(dataString);
    if (matcher.matches()) {
        System.out.println("Matched\n");
        System.out.println(matcher.group(1));
    }// w ww  . ja v  a  2s. c  om

    System.out.println(StringEscapeUtils.unescapeHtml(urlString));
}