Java Learning Note (string Supplement: Regular expression)

Source: Internet
Author: User

Just like the Python re module, but there are some differences between Java and Python regular expressions, here is a brief introduction, specific details can refer to other articles on the Web:

Function: can be used to verify a string, such as verifying the user name, verifying the password format, verifying whether it is a mailbox, etc.

Example:

"[0-9]{6,12}" matching rules: 6 to 12 digits, such as 123456789 is true,12345 is False

"1[3578][0-9]{9}" matching rules: 1 starts, the second bit is one of 3578, followed by nine arbitrary numbers, such as 13345678912 is true

"A*b" matching rule: after multiple A or 0 A, a b,b must be the last character, such as Aaaaab is TRUE,ABC or false

There are several methods in the string class that are not mentioned in the previous article, which is used here

Basic knowledge no longer introduced, examples:

Matches method:

 Packagedemo; Public classRegexdemo { Public Static voidMain (string[] args) {//Check QQ number, not 0 start, full number, 5 to 10 bitcheckqq (); //Check the phone number, 1 start, the second is 34578, a total of 11Checktel (); }     Public Static voidcheckqq () {String QQ= "123456"; Booleanb = Qq.matches ("[1-9][\\d]{4,9}"); System.out.println (b);//true    }     Public Static voidChecktel () {String Telnumber= "13312345678"; Booleanb = telnumber.matches ("1[34578][\\d]{9}"); System.out.println (b);//true    }}

Split method:

 Packagedemo; Public classRedexdemo { Public Static voidMain (string[] args) {split_1 ();        Split_2 ();    Split_3 (); }         Public Static voidsplit_1 () {String str= "I Love java"; String[] Strarr= Str.split ("+");  for(inti = 0; i < strarr.length; i++) {System.out.println (strarr[i]); }        //Print out: I love Java    }         Public Static voidsplit_2 () {String str= "10-12-45-78-62"; String[] Strarr= Str.split ("-");  for(inti = 0; i < strarr.length; i++) {System.out.println (strarr[i]); }        //Print out: Ten    }         Public Static voidSplit_3 () {String str= "192.168.160.1"; String[] Strarr= Str.split ("\ \."));  for(inti = 0; i < strarr.length; i++) {System.out.println (strarr[i]); }        //Print out: 192 168 1    }}

ReplaceAll Method:

 Packagedemo; Public classRedexdemo { Public Static voidMain (string[] args) {replaceall_1 (); }         Public Static voidReplaceall_1 () {String str= "Hello666123ilove789java"; String str1= Str.replaceall ("[\\d+]", "#");        System.out.println (STR1); //output: hello##### #Ilove # # #javaString str2= Str.replaceall ("[\\d]+", "#");        System.out.println (STR2); //Output: Hello#ilove#java    }}

Regular expression Exercises:

 Packagedemo; Public classRedexdemo { Public Static voidMain (string[] args) {//Check your email addressCheckmail (); }     Public Static voidCheckmail () {String email= "[Email protected]"; Booleanb = Email.matches ("[A-za-z0-9_][email protected][0-9a-z]+ (\\.[ a-z]+) + "); System.out.println (b);//true    }}

Java Learning Note (string Supplement: Regular expression)

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.