Java Study Notes 21 (String supplement: Regular Expression), learning notes string

Source: Internet
Author: User

Java Study Notes 21 (String supplement: Regular Expression), learning notes string

Just like the re module of python, there are some differences between the regular expressions of Java and Python. Here is a brief introduction. For details, refer to other articles on the Internet:

Function: it can be used to check a string, such as verifying the user name, verifying the password format, verifying whether it is a mailbox, and so on.

Example:

"[0-9] {6, 12}" matching rule: 6 to 12 digits. For example, if 123456789 is true, 12345 is false.

"1 [3578] [0-9] {9}" matching rule: starts with 1, and the second digit is one of 3578, followed by nine arbitrary numbers. For example, 13345678912 is true.

"A * B" matching rule: There is a B after multiple a or 0 a, and B must be the last character. For example, aaaaab is true, abc is false.

 

There are several methods in the String class that are not mentioned in the previous article.

Basic knowledge is not introduced. Example:

Matches method:

Package demo; public class RegexDemo {public static void main (String [] args) {// check the QQ number. It cannot start with 0, full number, 5 to 10 checkQQ (); // check the mobile phone number, starting with 1, and the second digit is 34578. A total of 11 checkTel ();} public static void checkQQ () {String QQ = "123456"; boolean B = QQ. matches ("[1-9] [\ d] {4, 9}"); System. out. println (B); // true} public static void checkTel () {String telNumber = "13312345678"; boolean B = telNumber. matches ("1 [34578] [\ d] {9}"); System. out. println (B); // true }}

 

Split method:

Package demo; public class RedexDemo {public static void main (String [] args) {split_1 (); split_2 (); split_3 ();} public static void split_1 () {String str = "I love java"; String [] strArr = str. split ("+"); for (int I = 0; I <strArr. length; I ++) {System. out. println (strArr [I]);} // print out: I love java} public static void split_2 () {String str = "10-12-45-78-62 "; string [] strArr = str. split ("-"); for (int I = 0; I <strArr. length; I ++) {System. out. println (strArr [I]);} // print: 10 12 45 78 62} public static void split_3 () {String str = "192.168.160.1 "; string [] strArr = str. split ("\\. "); for (int I = 0; I <strArr. length; I ++) {System. out. println (strArr [I]);} // print: 192 168 160 1 }}

 

 

ReplaceAll method:

Package demo; public class RedexDemo {public static void main (String [] args) {replaceAll_1 ();} public static void replaceAll_1 () {String str = "Hello666123Ilove789java "; string str1 = str. replaceAll ("[\ d +]", "#"); System. out. println (str1); // output: Hello ###### Ilove ### java String str2 = str. replaceAll ("[\ d] +", "#"); System. out. println (str2); // output: Hello # Ilove # java }}

 

 

Regular Expression exercises:

 

Package demo; public class RedexDemo {public static void main (String [] args) {// check the email address checkMail ();} public static void checkMail () {String email = "abc123@sina.com.cn"; boolean B = email. matches ("[a-zA-Z0-9 _] + @ [0-9a-z] + (\\. [a-z] +) + "); System. out. println (B); // true }}

 

Related Article

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.