Four common functions of Java Regular Expressions
Regular expressions have powerful functions in string processing. Sun added support for them in jdk1.4.
The following describes four common functions:
Query:
The following is a code snippet:
String STR = "abc efg abc ";
String RegEx = "A | f"; // indicates a or F
Pattern P = pattern. Compile (RegEx );
Matcher M = P. matcher (STR );
Boolean rs = M. Find ();
If STR contains RegEx, RS is true; otherwise, flase is used. If you want to ignore case sensitivity during search, you can write pattern P = pattern. Compile (RegEx, pattern. case_insensitive );
Extraction:
The following is a code snippet:
String RegEx = ". +/(. +) $ ";
String STR = "C:/dir1/dir2/name.txt ";
Pattern P = pattern. Compile (RegEx );
Matcher M = P. matcher (STR );
Boolean rs = M. Find ();
For (INT I = 1; I <= M. groupcount (); I ++ ){
System. Out. println (M. Group (I ));
}
The preceding execution result is name.txt, and the extracted string is stored in M. Group (I), where the maximum I value is M. groupcount ();
Split:
The following is a code snippet:
String RegEx = "::";
Pattern P = pattern. Compile (RegEx );
String [] r = P. Split ("XD: ABC: CDE ");
After the execution, R is {"XD", "ABC", "CDE"}. In fact, there are still simple methods to split the file:
String STR = "XD: ABC: CDE ";
String [] r = Str. Split ("::");
Replace (delete ):
The following is a code snippet:
String RegEx = "A +"; // indicates one or more
Pattern P = pattern. Compile (RegEx );
Matcher M = P. matcher ("aaabbced A ccdeaa ");
String S = M. replaceall ("");
The result is "abbced A ccdea"
If it is written as an empty string, the deletion function can be achieved, for example:
String S = M. replaceall ("");
The result is "bbced ccde"
Appendix:
/D is equal to [^ 0-9] non-Numbers
/S equals to [/T/N/x0b/F] blank characters
/S is equal to [^/T/N/x0b/F] non-blank characters
/W equals to [a-zA-Z_0-9] numbers or English letters
/W and so on [^ a-zA-Z_0-9] non-numbers and English text
^ Indicates the beginning of each line
$ Indicates the end of each line