Four common functions of Java Regular Expressions

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.