原创 Java Gossip: 使用正则表达式

2009-3-20 18:39 2126 4 4 分类: 软件与OS

import java.util.Scanner;
 
public class UseRegularExpression { 
    public static void main(String args[]) { 
        Scanner scanner = new Scanner(System.in);
 
        String str = "abcdefgabcabc"; 
        System.out.println(str.replaceAll(".bc", "###")); 
 
        System.out.print("手机: "); 
        str = scanner.next();
 
        if(str.matches("[0-9]{4}-[0-9]{6}")) 
            System.out.println("Yes"); 
        else 
            System.out.println("No");
 
        System.out.print("href标签: "); 
        str = scanner.next() + " " + scanner.next();
 
        if(str.matches("<a.+href*=*['\"]?.*?['\"]?.*?>"))
            System.out.println("Yes"); 
        else
            System.out.println("No");  
        System.out.print("邮箱: "); 
        str = scanner.next();
 
        if(str.matches("^[_a-z0-9-]+([.][_a-z0-9-]+)*@[a-z0-9-]+([.][a-z0-


9-]+)*$"))
            System.out.println("Yes"); 
        else
            System.out.println("No"); 
    } 
}


 


C:\Documents and Settings\Administrator\java>java UseRegularExpression
###defg######
手机: 0988-100432
Yes
href标签: ^P^PDBUFSQJMMBS^OPOMZGVO^OOFU^C^_
 asd
No
邮箱:
justin@cat.onlyfun.net
Yes


 


 

文章评论0条评论)

登录后参与讨论
我要评论
0
4
关闭 站长推荐上一条 /2 下一条