SAMPLE : Java String.matches()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/**
 * Copyright 2013. ABN Software. All Rights reserved.<br>
 * <br>
 * Homepage .... http://www.ABNsoft.info<br>
 * <br>
 * Project ..... ABNTest<br>
 * Package ..... ABNTest<br>
 * File name ... StringMatch.java<br>
 * <br>
 * Author ...... Andre<br>
 * Created ..... 18.04.2013<br>
 * <br>
 */

package info.abnsoft.java.trying;

import java.util.regex.Pattern;

/**
 * @author annik
 *
 */

public class StringMatch
{
    public static void main(final String[] args) {
        //
        final StringBuffer sb = new StringBuffer( "<response>\n"+
"    <category>client</category>\n"+
"    <action>Login</action>\n"+
"    <code>1000</code>\n"+
"    <msg>Command completed successfully</msg>\n"+
"    <value>L115:no value</value>\n"+
"    <resData>\n"+
"    </resData>\n"+
"    <cltrid>RM.2013.04.18.1366294770248</cltrid>\n"+
"    <svtrid>RM.2013.04.18.1366294770248-API-SRV</svtrid>\n"+
"    <chksum>8fc683ce8d7772f91ac28224fa3e6cc0</chksum>\n"+
"</response>\n" );
       
        System.out.println(sb.toString());
       
        System.out.println("\n\n======================");
        final String pattern0 = "(?s).*" + "</response>" + ".*";
        System.out.println(pattern0);
       
        if (sb.toString().toLowerCase().matches(pattern0)) {
            System.out.println("MATCH");
        }else {
            System.out.println(" ! FAILED");
        }
       
        System.out.println("\n\n======================");
        final String pattern1 = "(?m)(?s)" + "</response>" + "";
        System.out.println(pattern1);
       
        if (sb.toString().toLowerCase().matches(pattern1)) {
            System.out.println("MATCH");
        }else {
            System.out.println(" ! FAILED");
        }
       
        System.out.println("\n\n======================");
        final String pattern2 = "" + "</response>" + "";
        final Pattern p = Pattern.compile(pattern2, Pattern.MULTILINE+Pattern.DOTALL);
        System.out.println(p.matcher(sb.toString().toLowerCase()).find());  //true


    }
}

that code above will print follow :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<response>
    <category>client</category>
    <action>Login</action>
    <code>1000</code>
    <msg>Command completed successfully</msg>
    <value>L115:no value</value>
    <resData>
    </resData>
    <cltrid>RM.2013.04.18.1366294770248</cltrid>
    <svtrid>RM.2013.04.18.1366294770248-API-SRV</svtrid>
    <chksum>8fc683ce8d7772f91ac28224fa3e6cc0</chksum>
</response>



======================
(?s).*</response>.*
MATCH


======================
(?m)(?s)</response>
 ! FAILED


======================
true