Java Regular Expression matching, replacement, search, and cutting

Source: Internet
Author: User
Import java. util. arraylist;
Import java. util. RegEx. matcher;
Import java. util. RegEx. pattern;

Public class test {
Public static void main (string [] ARGs ){
Getstrings (); // use a regular expression to obtain the specified content in the specified string.
System. Out. println ("********************");
Replace (); // replace string content with a regular expression
System. Out. println ("********************");
Strsplit (); // use a regular expression to cut a string
System. Out. println ("********************");
Strmatch (); // String Matching
}

Private Static void strmatch (){
String phone = "13539770000 ";
// Check whether the phone number is a qualified mobile phone number (Standard: 1, the second digit is 3, 5, 8, and the last nine digits are any numbers)
System. Out. println (Phone + ":" + phone. Matches ("1 [358] [0-9] {9,9}"); // true

String STR = "abcd12345efghijklmn ";
// Check if STR contains 12345
System. Out. println (STR + ":" + Str. Matches ("\\ W + 12345 \ W +"); // true
System. Out. println (STR + ":" + Str. Matches ("\\ W + 123456 \ W +"); // false
}

Private Static void strsplit (){
String STR = "asfasf. sdfsaf. sdfsdfas. asdfasfdasfd. wrqwrwqer. asfsafasf. safgfdgdsg ";
String [] STRs = Str. Split ("\\.");
For (string S: STRs ){
System. Out. println (s );
}
}

Private Static void getstrings (){
String STR = "success ";
Pattern P = pattern. Compile ("QQ (.*?) QQ ");
Matcher M = P. matcher (STR );
Arraylist <string> STRs = new arraylist <string> ();
While (M. Find ()){
STRs. Add (M. Group (1 ));
}
For (string S: STRs ){
System. Out. println (s );
}
}

Private Static void Replace (){
String STR = "asfas5fsaf5s4fs6af. sdaf. ASF. wqre. QWR. fdsf. ASF ";
// Replace "." In the string with "_". Because "." is a special character, it must be expressed by "\." and "\" is a special character. Therefore, it must be expressed.
STR = Str. replaceall ("\\.","_");
System. Out. println (STR );
}
}

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.