"Java Regular expression synthesis exercise"

Source: Internet
Author: User

One, the treatment of stuttering.

will be the string "I i i i i i i i I ...." I....... If you want to ....... ....... If you want to ... Learning to learn the breeze .... Breezy programming Cheng ..... Cheng "into" I want to learn programming "

Analysis: First put .... Remove: Use "\\.+" mode, then replace the overlapping words with one: use "(.) \\1+ "mode.

Code:

1  PackageP02. Exercise;2 3  Public classDemo01 {4      Public Static voidMain (String args[])5     {6String str= "I i i i i i i i I .... I....... If you want to ....... ....... If you want to ... Learning to learn the breeze .... Breezy programming Cheng ..... Cheng ";7         //1. Remove first.8String regex= "\\.+";9Str=str.replaceall (Regex, "");Ten //System.out.println (str); One         //2. Merge overlapping words.  ARegex= "(.) \\1+ "; -Str=str.replaceall (Regex, "$"); - System.out.println (str); the     } -}
View Code

Operation Result:

I want to learn to program

Second, the IP address sort.

Sorts the output of several IP addresses.

"192.168.10.34 127.0.0.1 105.70.11.75 3.3.3.3"

Idea: Sorting is done automatically using TreeSet, but sorting is sorted according to the dictionary order of the string, to order by size, need to add 0, the output needs to be removed 0.

Code:

1  PackageP02. Exercise;2 3 ImportJava.util.TreeSet;4 5  Public classDemo02 {6      Public Static voidMain (String args[])7     {8String str= "192.168.10.34 127.0.0.1 105.70.11.75 3.3.3.3";9         //1. Split the IP address first. TenString regex= "+"; OneString arr[]=str.split (regex); A          for(String S:arr) - System.out.println (s); - System.out.println (); the System.out.println (); -      -         //2. For each byte segment less than 3 bits of the 0, first fill two 0 -Regex= "(\\d+)"; +          for(inti=0;i<arr.length;i++) -         { +Arr[i]=arr[i].replaceall (Regex, "00$1"); A         } at         //3. Remove the excess 0 and keep only three bits.  -Regex= "0* (\\d{3})"; -          for(inti=0;i<arr.length;i++) -         { -Arr[i]=arr[i].replaceall (Regex, "$"); - System.out.println (Arr[i]); in         } - System.out.println (); to System.out.println (); +         //4. Put the TreeSet collection to sort.  -TreeSet <string>ts=NewTreeset<string>(); the          for(String S:arr) *         { $ Ts.add (s);Panax Notoginseng         } -Regex= "0* (\\d+)"; the          for(String s:ts) +         { ASystem.out.println (S.replaceall (Regex, "$")); the         } +     } -}
View Code

Operation Result:

1 192.168.10.34 2 127.0.0.1 3 105.70.11.75 4 3.3.3.3 5  6  7 192.168.010.034 8 127.000.000.001 9 105.070.011.075 003.003.003.003  A  3.3.3.3 105.70.11.75 127.0.0.1 192.168.10.34 
View Code

When the extra 0 is removed, the mode used is: "0* (\\d+)", so that even if all is 0, the last 0 can be retained at the end of the last use of the replacement.

Third, check the mailbox.

Mode used: "\\[email protected][a-za-z0-9]+ (\\.[ a-z]{2,3}) {"}", meaning the user identifier is alphanumeric underline, the length is not limited; mail server domain name the first word should be an alphanumeric composition, the rest should be made up of lowercase letters.

Code:

1  PackageP02. Exercise;2 3  Public classDemo03 {4      Public Static voidMain (String args[])5     {6String str= "[Email protected]";7String regex= "\\[email protected][a-za-z0-9]+ (\\.[ a-z]{2,3}) {"}";8 System.out.println (Str.matches (regex));9     }Ten}
View Code

"Java Regular expression synthesis exercise"

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.