java ---正则表达式
package sad; import java.util.Scanner; //正則表達式是字符串類型 import javax.management.loading.PrivateClassLoader; public class ers { public static void main(String[] args) { System.out.println("請輸入您要操作的字符串:"); Scanner sc = new Scanner(System.in); String str1 = sc.next(); Factory.switches(str1); } } class Factory { static int chance; static void switches(String str1) { Scanner sc = new Scanner(System.in); System.out.println("请输入您要进行的操作:"); System.out.print("\n\n"); System.out.println("1.普通方法確定字符串是否全由數字構成"); System.out.println("2.正則表達式確定字符串是否全由數字構成"); System.out.println("3.刪除字符串中的所有數字"); System.out.println("4:驗證郵箱格式是否正確"); chance = sc.nextInt(); switch(chance) { case 1: new NormalBoolean(str1); break; case 2: new ZhengzeBoolean(str1); break; case 3: new ExchangeNum(str1); break; case 4: new TestMail(str1); break; default: break; } } } class NormalBoolean { NormalBoolean(String str) //常规方法 { char[] arr = str.toCharArray(); for(int i=0;i<arr.length;i++) { if(arr[i]<'0'||arr[i]>'9') { System.out.println("不是全由数字构成"); } } System.out.println("是全由数字构成"); } } class ZhengzeBoolean //正则表达式 { public ZhengzeBoolean(String str1) { if(str1.matches("\\d+")) { System.out.println("正则验证是由数字构成"); } else { System.out.println("正则验证不是由数字构成"); } } } class ExchangeNum //刪除字符串中的數字 { public ExchangeNum(String str) { // TODO Auto-generated constructor stub exchange(str); } private void exchange(String str) { String regex = "[0-9]+"; //String regex = "\\d++"; System.out.println(str.replaceAll(regex,"*")); //可以將數字替換成其他字符 } } class TestMail //驗證郵箱格式是否正確 *****@****.**** { public TestMail(String Str) { Mail(Str); } private void Mail(String Str) { String regex = "\\w+@\\w+\\.\\w+"; String regex1= "[1]"; System.out.println(Str.matches(regex1)); } }