Java Regular Expression test case

Source: Internet
Author: User

(1) Sort the IP address:

1  Public Static voidtest_1 () {2String ip_str = "192.168.10.34 127.0.0.1 3.3.3.3 105.70.11.55";3         //1, in order for IP to be compared in string order, as long as the number of bits per segment of IP is the same. 4         //So, fill 0, according to each one need to do more than 0 to supplement. Each paragraph is added two 0.5Ip_str = Ip_str.replaceall ("(\\d+)", "00$1");6 System.out.println (IP_STR);7         //then each paragraph retains the number 3 bits. 8Ip_str = Ip_str.replaceall ("0* (\\d{3})", "$");9 System.out.println (IP_STR);Ten         //1, the IP address is cut out.  OneString ips[] = ip_str.split ("+"); Atreeset<string> ts =NewTreeset<string>(); -          for(String ip:ips) { - ts.add (IP); the         } -         //re-cut the four parts of a field one time by rule -          for(String ip:ts) { -System.out.println (Ip.replaceall ("0* (\\d+)", "$")); +         } -}

Operation Result:

00192.00168.0010.0034   00127.000.000.001 003.003.003.003 00105.0070.0011.0055 192.168.010.034   127.000.000.001 003.003.003.003 105.070.011.055 3.3.3.3105.70.11.55127.0.0.1192.168.10.34

(2) String interception:

1  Public Static voidtest_2 () {2String St1 = "This today .... This is the present day ..... Days.... Everyday every day .... Every day. Learn to learn, learn to learn and learn. Learn ... Breezy ";3         //put the "." In the string Remove4String regex = "\\.+";5St1 = St1.replaceall (Regex, "");6 System.out.println (ST1);7         //Remove overlapping words8St1 = St1.replaceall ("(.) \\1+ "," $ ");9 System.out.println (ST1);Ten}

Operation Result:

Today and now every day everyday everyday to learn to learn to learn to learn to learn learning to study the Breeze Breeze today study

(3) Check the email address:

1   Public Static voidTest_3 () {2String mail = "[Email protected]";3 4String regex = "[A-za-z0-9_][email protected][a-za-z0-9]+ (\\.[ a-za-z]{1,3}) + ";5 6Regex = "\\[email protected]\\w+ (\\.\\w+) +";//[email protected]7 8   Booleanb =mail.matches (regex);9 TenSystem.out.println (Mail + ":" +b); One}

Operation Result:

[Email protected]:true

Java Regular Expression test case

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.