关闭

正则表达式的举例

224人阅读 评论(0) 收藏 举报
分类:
public static void main(String[] args) 
{
String[] mails =
{
"kongyeeku@163.com" ,
"kongyeeku@gmail.com",
"ligang@crazyit.org",
"wawa@abc.xx"
};
String mailRegEx = "\\w{3,20}@\\w+\\.(com|org|cn|net|gov)";
Pattern mailPattern = Pattern.compile(mailRegEx);
Matcher matcher = null;
for (String mail : mails)
{
if (matcher == null)
{
matcher = mailPattern.matcher(mail); 
}
else
{
matcher.reset(mail);
}
String result = mail + (matcher.matches() ? "是" : "不是")
+ "一个有效的邮件地址!";
System.out.println(result);
}

}


输出结果:

truekongyeeku@163.com是一个有效的邮件地址!
kongyeeku@gmail.com是一个有效的邮件地址!
ligang@crazyit.org是一个有效的邮件地址!
wawa@abc.xx不是一个有效的邮件地址!




package com.enum1;




import java.io.*;
import java.util.*;
/**
 * Description:
 * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a> 
 * <br/>Copyright (C), 2001-2012, Yeeku.H.Lee
 * <br/>This program is protected by copyright laws.
 * <br/>Program Name:
 * <br/>Date:
 * @author Yeeku.H.Lee kongyeeku@163.com
 * @version 1.0
 */
public class SystemTest
{
public static void main(String[] args) throws Exception
{
String[] msgs =
{
"Java has regular expressions in 1.4", 
"regular expressions now expressing in Java", 
"Java represses oracular expressions" 
};
for (String msg : msgs)
{
System.out.println(msg.replaceFirst("re\\w*" , "哈哈:)"));
System.out.println(Arrays.toString(msg.split(" ")));
}

}



哈哈:) expressions now expressing in Java
[regular, expressions, now, expressing, in, Java]
Java 哈哈:) oracular expressions
[Java, represses, oracular, expressions]

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:2566次
    • 积分:40
    • 等级:
    • 排名:千里之外
    • 原创:0篇
    • 转载:9篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档