search
尋找貓咪~QQ 地點 桃園市桃園區 Taoyuan , Taoyuan

[Java 教學範例拷貝]- 使用正則表示式(Regular expression) – jashliao部落格

[Java 教學範例拷貝]- 使用正則表示式(Regular expression)

 

剛才找資料時發現一個的Java 教學網站,趕快發揮(C/P)的長才將它備份來,有需要的同好,歡迎來(C/P)一下^^。

 

拷貝來源:
http://openhome.cc/Gossip/JavaGossip-V1/

http://openhome.cc/Gossip/JavaGossip-V1/RegularExpression.htm

 

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("格式正確");
else
System.out.println("格式錯誤");
System.out.print("輸入href標籤: ");
// Scanner的next()方法��以空白為區隔
// 我們的輸入有空白,所以要執行兩次
str = scanner.next() + " " + scanner.next();
// 驗證href標籤 
if(str.matches("\"]?.*?['\"]?.*?>"))
System.out.println("格式正確");
else
System.out.println("格式錯誤");
System.out.print("輸入電子郵件: ");
str = scanner.next();
// 驗證電子郵件格式 
if(str.matches(
"^[_a-z0-9-]+([.][_a-z0-9-]+)*@[a-z0-9-]+([.][a-z0-9-]+)*$"))
System.out.println("格式正確");
else
System.out.println("格式錯誤");
}
}


熱門推薦

本文由 jashliaoeuwordpress 提供 原文連結

寵物協尋 相信 終究能找到回家的路
寫了7763篇文章,獲得2次喜歡
留言回覆
回覆
精彩推薦