java ---Regex

來源:互聯網
上載者:User

標籤:for   數字   private   驗證   str1   test   system.in   todo   int   

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    //Regex{      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 = "\\[email protected]\\w+\\.\\w+";        String regex1= "[1]";        System.out.println(Str.matches(regex1));    }}

 

java ---Regex

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.