合法E-mail地址: 1. 必須包含一個並且只有一個符號“@” 2. 第一個字元不得是“@”或者“.” 3. 不允許出現“@.”或者.@ 4. 結尾不得是字元“@”或者“.” 5. 允許“@”前的字元中出現“+” 6. 不允許“+”在最前面,或者“+@” Regex如下: ----------------------------------------------------------------------- ^(\w+((-\w+)|(\.\w+))*)\+\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$ ----------------------------------------------------------------------- 字元描述: ^ :匹配輸入的開始位置。 \:將下一個字元標記為特殊字元或字面值。 * :匹配前一個字元零次或幾次。 + :匹配前一個字元一次或多次。 (pattern) 與模式比對並記住匹配。 x|y:匹配 x 或 y。 [a-z] :表示某個範圍內的字元。與指定區間內的任何字元匹配。 \w :與任何單詞字元匹配,包括底線。 $ :匹配輸入的結尾。
//電子郵件 String check = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$"; Pattern regex = Pattern.compile(check); Matcher matcher = regex.matcher("dffdfdf@qq.com"); boolean isMatched = matcher.matches(); System.out.println(isMatched); /* 電話號碼 String check = "^(13[4,5,6,7,8,9]|15[0,8,9,1,7]|188|187)\\d{8}$"; Pattern regex = Pattern.compile(check); Matcher matcher = regex.matcher("13555655606"); boolean isMatched = matcher.matches(); System.out.println(isMatched); */
或使用運算式"^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$"
RegexBuddy的預設檢查Email方式為:“\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}\b”