Import java. util. regex. Matcher;
Import java. util. regex. Pattern;
Public class Test {
Public static void main (String [] args ){
// A simple understanding of the concept of Regular Expressions
/*
P ("abc". matches ("..."));
P ("a8729a". replaceAll ("\ d ","-"));
Pattern p = Pattern. compile ("[a-z] {3 }");
Matcher m = p. matcher ("fgh ");
P (m. matches ());
P ("fgha". matches ("[a-z] {3 }"));
*/
// Preliminary understanding. * +?
/*
P ("a". matches ("."));
P ("aa". matches ("aa "));
P ("aaaa". matches ("*"));
P ("aaaa". matches ("a + "));
P ("". matches ("*"));
P ("aaaa". matches ("? "));
P ("". matches ("? "));
P ("a". matches ("? "));
P ("214523145234532". matches ("\ d {3,100 }"));
P ("192.168.0.aaa ". matches ("\ d {1, 3 }\\. \ d {1, 3 }\\. \ d {1, 3 }\\. \ d {1, 3 }"));
("192". matches ("[0-2] [0-9] [0-9]");
*/
// Range
/*
P ("a". matches ("[abc]");
P ("a". matches ("[^ abc]");
P ("A". matches ("[a-zA-Z]");
P ("A". matches ("[a-z] | [A-Z]");
P ("A". matches ("[a-z [A-Z]");
P ("R". matches ("[A-Z & [RFG]");
*/
// Recognize \ s \ w \ d \
/*
P ("\ n \ r \ t". matches ("\ s {4 }"));
P ("". matches ("\ S "));
P ("a_8". matches ("\ w {3 }"));
P ("abc888 & ^ %". matches ("[a-z] {1, 3} \ d + [& ^ # %] + "));
P ("\". matches ("\\\\"));
*/
// POSIX Style
// P ("a". matches ("\ p {Lower }"));
// Boundary
/*
P ("hello sir". matches ("^ h .*"));
P ("hello sir". matches (". * ir $ "));
P ("hello sir". matches ("^ h [a-z] {1, 3} o \ B .*"));
P ("hellosir". matches ("^ h [a-z] {1, 3} o \ B .*"));
// Whilte lines
P ("\ n". matches ("^ [\ s & [^ \ n] * \ n $ "));
P ("aaa 8888c". matches (". * \ d {4 }."));
P ("aaa 8888c". matches (". * \ B \ d {4 }."));
P ("aaa8888c". matches (". * \ d {4 }."));
P ("aaa8888c". matches (". * \ B \ d {4 }."));
*/
// Email
// P ("asdfasdfsafsf@dsdfsdf.com ". matches ("[\ w [. -] + @ [\ w [. -] + \\. [\ w] + "));
// Matches find lookingAt
/*
Pattern p = Pattern. compile ("\ d {3, 5 }");
String s = "123-34345-234-00 ";
Matcher m = p. matcher (s );
P (m. matches ());
M. reset ();
P (m. find ());
P (m. start () + "-" + m. end ());
P (m. find ());
P (m. start () + "-" + m. end ());
P (m. find ());
P (m. start () + "-" + m. end ());
P (m. find ());
// P (m. start () + "-" + m. end ());
P (m. lookingAt ());
P (m. lookingAt ());
P (m. lookingAt ());
P (m. lookingAt ());
*/
// Replacement
/*
Pattern p = Pattern. compile ("java", Pattern. CASE_INSENSITIVE );
Matcher m = p. matcher ("java Java JAVa JaVa IloveJAVA you hateJava afasdfasdf ");
StringBuffer buf = new StringBuffer ();
Int I = 0;
While (m. find ()){
I ++;
If (I % 2 = 0 ){
M. appendReplacement (buf, "java ");
} Else {
M. appendReplacement (buf, "JAVA ");
}
}
M. appendTail (buf );
P (buf );
*/
// Group
/*
Pattern p = Pattern. compile ("(\ d {3, 5}) ([a-z] {2 })");
String s = "123aa-34345bb-234cc-00 ";
Matcher m = p. matcher (s );
While (m. find ()){
P (m. group ());
}
*/
// Qulifiers
/*
Pattern p = Pattern. compile (". {3, 10} + [0-9]");
String s = "aaaa5bbbb68 ";
Matcher m = p. matcher (s );
If (m. find ())
P (m. start () + "-" + m. end ());
Else
P ("not match! ");
*/
// Non-capturing groups
/*
Pattern p = Pattern. compile (". {3 }(? = )");
String s = "444a66b ";
Matcher m = p. matcher (s );
While (m. find ()){
P (m. group ());
}
*/
// Back refenrences
/*
Pattern p = Pattern. compile ("(\ d) \ 2 ");
String s = "122 ";
Matcher m = p. matcher (s );
P (m. matches ());
*/
// Flags
// Pattern p = Pattern. compile ("java", Pattern. CASE_INSENSITIVE );
P ("Java". matches ("(? I) (java )"));
}
Public static void p (Object o ){
System. out. println (o );
}
}