Java regular expression-match character acquisition and IP address sorting

Source: Internet
Author: User

 

Package RegEx; import Java. util. regEx. *; import Java. util. *; public class regexget {public static void main (string ARGs []) {// get (); // test_1 (); test_2 ();} public static void get () {string STR = "Good good study, day up"; string RegEx = "\ B [a-Z] {4} \ B "; // take the word pattern P = pattern, which consists of four characters. compile (RegEx); // system. out. println (p); matcher M = P. matcher (STR); While (M. find () {system. out. println (M. group () + M. start () + "---" + M. end () ;}}/** Exercise 1 * Will me ...... to be ....... learning ...... learning ....... compile .......... process to * I want to learn programming **/public static void test_1 () {string STR = "I am ...... to be ....... learning ...... learning ....... compile .......... cheng "; // replace all vertices in string RegEx = "\\. + "; STR = Str. replaceall (RegEx, ""); // The following will repeat only one RegEx = "(.) \ 1 + "; STR = Str. replaceall (RegEx, "$1"); system. out. println (STR);}/* Exercise 2 * sorts the given IP addresses * 192.68.1.254 102.49.23.13 10.10.10.10 2.2.2.2. 8.109.90.30 **/public static void test_2 () {string IP = "192.68.1.254 102.49.23.13 10.10.10.10 2.2.2.2 8.109.90.30 "; // Add 0 string RegEx = "(\ D +)" to each IP bit first; IP = IP. replaceall (RegEx, "00 $1"); // retain only three RegEx = "0 * (\ D {3})"; IP = IP. replaceall (RegEx, "$1"); // cut string [] result = IP. split ("+"); arrays. sort (result); system. out. println ("after"); For (INT I = 0; I <result. length; I ++) result [I] = Result [I]. replaceall ("0 * (\ D +)", "$1"); // remove the previous 0 for (string S: result) system. out. println (s );}}

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.