Importjava.util.Arrays;ImportJava.util.Comparator;ImportJava.util.Scanner;ImportJava.util.regex.Matcher;ImportJava.util.regex.Pattern;classMycomparatorImplementsComparator<string>{ Public intCompare (String ip1, String ip2) {Pattern P= Pattern.compile ("\\d{1,3}"); Matcher M1=P.matcher (IP1); Matcher m2=P.matcher (IP2); while(M1.find ()) {m2.find (); System.out.println (M1.group ()+ " " +M2.group ()); intI1 =integer.valueof (M1.group ()); intI2 =integer.valueof (M2.group ()); if(I1 < I2)return-1; Else if(I1 > I2)return1; } return0; } } Public classRegexdemo {/** * @paramargs*/ Public Static voidMain (string[] args) {/** Treat stuttering: "I am." I-I-I ... I-I want to ... Want to. If you want to learn to learn ... Learn to learn to learn to make up a series of ... Coding programming Regulation regulation. Ride.. Regulation Regulation ... Regulation regulation "* into" I want to learn to program "* 1. Replace 2. Processing overlapping words*/String str= "I am." I-I-I ... I-I want to ... Want to. If you want to learn to learn ... Learn to learn to learn to make up a series of ... Coding programming Regulation regulation. Ride.. Regulation Regulation ... Process. Regulation Regulation "; STR= Str.replaceall ("\\.+", "" "); STR= Str.replaceall ("(.) \\1+ "," $ "); System.out.println (str); /** Order of IP addresses: * 192.168.10.34 127.0.0.1 3.3.3. 3 105.70.11.55 * Number comparison of corresponding segments! * */ //The first method, which gets the number of each field, is compared between the numbersstring[] IP = {"192.168.10.34", "127.0.0.1", "3.3.3.3", "105.70.11.55"}; Arrays.sort (IP,Newmycomparator ()); for(inti=0; i<ip.length; ++i) System.out.println (Ip[i]); //The second way, the length of each field is changed to 3 bitsString[] Ip2 = {"192.168.10.34", "127.0.0.1", "3.3.3.3", "105.70.11.55"}; for(inti=0; i<ip2.length; ++i) {Ip2[i]= Ip2[i].replaceall ("(\\d+)", "00$1");//First, each paragraph is preceded by a preamble 0Ip2[i] = Ip2[i].replaceall ("0* (\\d{3})", "$");//turn each section into a standard 3-bit} arrays.sort (IP2); for(inti=0; i<ip2.length; ++i) {Ip2[i]= Ip2[i].replaceall ("0* (\\d+)", "$");//restore the numbers for each segment! System.out.println (Ip2[i]); } /** Check the email address! */String Mail= "[Email protected]"; BooleanBF = Mail.matches ("\\[email protected]\\w+ (\\.\\w+) +"); System.out.println (Mail+ ":" +BF); String SR= "fsdfs.12345"; Pattern P= Pattern.compile ("\\w+"); Matcher m=P.matcher (SR); while(M.find ()) {System.out.println (M.group ()); } /** Regular expression is to realize the IP address of the inspection function! */String IP3=NULL; String Ipregex= "^ (\\d| [1-9]\\d|1\\d*|2[0-4]\\d|25[0-5]) (\\.\\1) {3}$ "; /** \\d| [1-9]\\D|1\\D*|2[0-4]\\D|25[0-5] * The number of the paragraph is only one time, two digits when the three-digit time (1 starts with 2)*/Scanner Scan=NewScanner (system.in); while((IP3 = Scan.nextline ())! =NULL){ if(Ip3.matches (Ipregex)) System.out.println ("True"); ElseSystem.out.println ("false"); } }}
Java Regular Expression small exercise (IP address detection, sorting, processing of overlapping words, access to email addresses)