[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 ));
}
}