Regular Expression exercises

Source: Internet
Author: User

[Java]
/*
* Start the program header annotation.
* Copyright and version Declaration of the program
* Copyright (c) 2011, a student from the computer College of Yantai University
* All rights reserved.
* File name: Regular Expression exercise
* Author: Xue Guangchen
* Completion date: January 1, November 17, 2012
* Version No.: x1.0

* Description of tasks and Solutions
* Input description:
* Problem description:
* Program output:
* End the comment in the program Header
*/
Import java. util .*;
Class RegexTest
{
Public static void main (String [] args)
{
// Test_1 ();
// IpSort ();
CheckMail ();
}
 
// Requirement:
// Convert the following string to: I want to learn programming.
Public static void test_1 ()
{
String str = "I am... I am... I want .. to... to... learning .... learning... editing... programming .. cheng. cheng... cheng... cheng ";
Str = str. replaceAll ("\. + ","");
/// System. out. println (str );
Str = str. replaceAll ("(.) \ 1 +", "$1 ");
System. out. println (str );
}
 
// 192.68.1.254 102.49.23.013 10.10.10.10 2.2.2.2 8.109.90.30
// Sort IP addresses in the order of CIDR blocks.
Public static void ipSort ()
{
String ip = "192.68.1.254 102.49.23.013 10.10.10.10 2.2.2.2 8.109.90.30 ";
Ip = ip. replaceAll ("(\ d +)", "00 $1 ");
// System. out. println (ip );
Ip = ip. replaceAll ("0 * (\ d {3})", "$1 ");
System. out. println (ip );
 
String [] arr = ip. split ("+ ");
TreeSet <String> ts = new TreeSet <String> ();
 
For (String str: arr)
{
Ts. add (str );
}
For (String str: ts)
{
System. out. println (str. replaceAll ("0 * (\ d +)", "$1 "));
}
}
 
// Requirement: Check the email address.
Public static void checkMail ()
{
String mail = "abc12@sina.com ";
String reg = "[a-zA-Z0-9 _] + @ [a-zA-Z0-9] + (\. [a-zA-Z] +) +"; // more precise matching.
Reg = "\ w + @ \ w + (\. \ w +) +"; // relatively inaccurate matching.
System. out. println (mail. matches (reg ));
}
}

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.