正則表達式通常被用于判斷語句中,用來檢查某一個字符串是否滿足某一格式。正則表達式是含有一些具有特殊意義字符的字符串。
幾種常用的正則表達式元字符:
. 代表任意一個字符
\\d 代表0-9的如何一個數(shù)字
\\D 代表任何一個非數(shù)字字符
\\s 代表空白字符 如'\t'、'\n'
\\S 代表非空白字符
\\w 代表可用于標識符的字符
\\W 代表不可用于標識符的字符
\\p{Lower} 代表小寫字母a-z
\\p{Upper} 代表大寫字母A-Z
常用的限定修飾符:
? 代表0次或1次
* 代表0次或多次
+ 代表1次或多次
{n} 代表正好出現(xiàn)n次
{n,} 代表至少出現(xiàn)n次
{n,m} 代表出現(xiàn)n-m次
代碼示例如下:文章來源:http://www.zghlxwxcb.cn/news/detail-479058.html
/**
* @author qx
* @date 2023/06/10
* @desc 正則表達式測試
*/
public class RegexDemo {
public static void main(String[] args) {
// 定義匹配email地址的正則表達式
String regex = "\\w+@\\w+(\\.\\w{2,3})*\\.\\w{2,3}";
String str1 = "aaa@";
String str2 = "aaaa";
String str3 = "hello@qq.com";
String str4 = "111@kfc.dfg.com";
System.out.println(str1.matches(regex));
System.out.println(str2.matches(regex));
System.out.println(str3.matches(regex));
System.out.println(str4.matches(regex));
}
}
執(zhí)行結(jié)果:文章來源地址http://www.zghlxwxcb.cn/news/detail-479058.html
false
false
true
true
到了這里,關于Java使用正則表達式-驗證郵箱的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!