Android中如何使用正則表達(dá)式,針對(duì)這個(gè)問(wèn)題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問(wèn)題的小伙伴找到更簡(jiǎn)單易行的方法。
新榮ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書未來(lái)市場(chǎng)廣闊!成為創(chuàng)新互聯(lián)公司的ssl證書銷售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:028-86922220(備注:SSL證書合作)期待與您的合作!
驗(yàn)證手機(jī)號(hào):
public class ClassPathResource { public static boolean isMobileNO(String mobiles) { Pattern p = Pattern .compile("^(([-])|([^,//D])|([,-]))//d{}$"); Matcher m = p.matcher(mobiles); System.out.println(m.matches() + "---"); return m.matches(); } public static void main(String[] args) throws IOException { System.out.println(ClassPathResource.isMobileNO("")); } } public class ClassPathResource { public static boolean isMobileNO(String mobiles) { Pattern p = Pattern .compile("^(([-])|([^,//D])|([,-]))//d{}$"); Matcher m = p.matcher(mobiles); System.out.println(m.matches() + "---"); return m.matches(); } public static void main(String[] args) throws IOException { System.out.println(ClassPathResource.isMobileNO("")); } }
驗(yàn)證郵箱:
public static boolean isEmail(String strEmail) { String strPattern = "^[a-zA-Z][\\w\\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]\\.[a-zA-Z][a-zA-Z\\.]*[a-zA-Z]$"; Pattern p = Pattern.compile(strPattern); Matcher m = p.matcher(strEmail); return m.matches(); } public static boolean isEmail(String strEmail) { String strPattern = "^[a-zA-Z][\\w\\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]\\.[a-zA-Z][a-zA-Z\\.]*[a-zA-Z]$"; Pattern p = Pattern.compile(strPattern); Matcher m = p.matcher(strEmail); return m.matches(); }
檢查EditText中輸入的是否符合規(guī)則:
import Android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; public class Main extends Activity { private EditText editText; private Button button; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); editText = (EditText) findViewById(R.id.textId); editText.setText("EditText element"); button = (Button) findViewById(R.id.btnId); button.setText("Check"); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (checkString(editText.getText().toString())) { editText.setText("Corect"); } } }); } private boolean checkString(String s) { return s.matches("\\w*[.](Java|cpp|class)"); } } import Android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; public class Main extends Activity { private EditText editText; private Button button; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); editText = (EditText) findViewById(R.id.textId); editText.setText("EditText element"); button = (Button) findViewById(R.id.btnId); button.setText("Check"); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (checkString(editText.getText().toString())) { editText.setText("Corect"); } } }); } private boolean checkString(String s) { return s.matches("\\w*[.](Java|cpp|class)"); } }
常用正則表達(dá)式收集
正則表達(dá)式用于字符串處理、表單驗(yàn)證等場(chǎng)合,實(shí)用高效?,F(xiàn)將一些常用的表達(dá)式收集于此,以備不時(shí)之需。
匹配中文字符的正則表達(dá)式: [\u4e00-\u9fa5]
評(píng)注:匹配中文還真是個(gè)頭疼的事,有了這個(gè)表達(dá)式就好辦了
匹配雙字節(jié)字符(包括漢字在內(nèi)):[^\x00-\xff]
評(píng)注:可以用來(lái)計(jì)算字符串的長(zhǎng)度(一個(gè)雙字節(jié)字符長(zhǎng)度計(jì)2,ASCII字符計(jì)1)
匹配空白行的正則表達(dá)式:\n\s*\r
評(píng)注:可以用來(lái)刪除空白行
匹配HTML標(biāo)記的正則表達(dá)式:<(\S*?)[^>]*>.*?|<.*? />
評(píng)注:網(wǎng)上流傳的版本太糟糕,上面這個(gè)也僅僅能匹配部分,對(duì)于復(fù)雜的嵌套標(biāo)記依舊無(wú)能為力
匹配首尾空白字符的正則表達(dá)式:^\s*|\s*
評(píng)注:可以用來(lái)刪除行首行尾的空白字符(包括空格、制表符、換頁(yè)符等等),非常有用的表達(dá)式
匹配Email地址的正則表達(dá)式:\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
評(píng)注:表單驗(yàn)證時(shí)很實(shí)用
匹配網(wǎng)址URL的正則表達(dá)式:[a-zA-z]+://[^\s]*
評(píng)注:網(wǎng)上流傳的版本功能很有限,上面這個(gè)基本可以滿足需求
匹配帳號(hào)是否合法(字母開頭,允許5-16字節(jié),允許字母數(shù)字下劃線):^[a-zA-Z][a-zA-Z0-9_]{4,15}
評(píng)注:表單驗(yàn)證時(shí)很實(shí)用
匹配國(guó)內(nèi)電話號(hào)碼:\d{3}-\d{8}|\d{4}-\d{7}
評(píng)注:匹配形式如 0511-4405222 或 021-87888822
匹配騰訊QQ號(hào):[1-9][0-9]{4,}
評(píng)注:騰訊QQ號(hào)從10000開始
匹配中國(guó)郵政編碼:[1-9]\d{5}(?!\d)
評(píng)注:中國(guó)郵政編碼為6位數(shù)字
匹配身份證:\d{15}|\d{18}
評(píng)注:中國(guó)的身份證為15位或18位
匹配ip地址:\d+\.\d+\.\d+\.\d+
評(píng)注:提取ip地址時(shí)有用
匹配特定數(shù)字:
^[1-9]\d* //匹配正整數(shù)
^-[1-9]\d* //匹配負(fù)整數(shù)
^-?[1-9]\d* //匹配整數(shù)
^[1-9]\d*|0 //匹配非負(fù)整數(shù)(正整數(shù) + 0)
^-[1-9]\d*|0 //匹配非正整數(shù)(負(fù)整數(shù) + 0)
^[1-9]\d*\.\d*|0\.\d*[1-9]\d* //匹配正浮點(diǎn)數(shù)
^-([1-9]\d*\.\d*|0\.\d*[1-9]\d*) //匹配負(fù)浮點(diǎn)數(shù)
^-?([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0) //匹配浮點(diǎn)數(shù)
^[1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0 //匹配非負(fù)浮點(diǎn)數(shù)(正浮點(diǎn)數(shù) + 0)
^(-([1-9]\d*\.\d*|0\.\d*[1-9]\d*))|0?\.0+|0 //匹配非正浮點(diǎn)數(shù)(負(fù)浮點(diǎn)數(shù) + 0)
評(píng)注:處理大量數(shù)據(jù)時(shí)有用,具體應(yīng)用時(shí)注意修正
匹配特定字符串:
^[A-Za-z]+ //匹配由26個(gè)英文字母組成的字符串
^[A-Z]+ //匹配由26個(gè)英文字母的大寫組成的字符串
^[a-z]+ //匹配由26個(gè)英文字母的小寫組成的字符串
^[A-Za-z0-9]+ //匹配由數(shù)字和26個(gè)英文字母組成的字符串
^\w+ //匹配由數(shù)字、26個(gè)英文字母或者下劃線組成的字符串
評(píng)注:最基本也是最常用的一些表達(dá)式
關(guān)于Android中如何使用正則表達(dá)式問(wèn)題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒(méi)有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。
分享文章:Android中如何使用正則表達(dá)式
文章地址:http://aaarwkj.com/article2/ijhcoc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、做網(wǎng)站、品牌網(wǎng)站設(shè)計(jì)、微信公眾號(hào)、品牌網(wǎng)站建設(shè)、小程序開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)