我想設置電子郵件輸入的定期驗證。使用正則表達式進行Java驗證
我有這樣的Java類,它確實驗證:
public class EmailValidator implements Validator {
@Override
public void validate(FacesContext facesContext,
UIComponent uIComponent, Object value) throws ValidatorException {
//Get the component's contents and cast it to a String
String enteredEmail = (String)value;
//Set the email pattern string
Pattern p = Pattern.compile("[email protected]+\\.[a-z]+");
//Match the given string with the pattern
Matcher m = p.matcher(enteredEmail);
//Check whether match is found
boolean matchFound = m.matches();
if (!matchFound) {
FacesMessage message = new FacesMessage();
message.setDetail("Email not valid - The email must be in the format [email protected]");
message.setSummary("Email not valid - The email must be in the format [email protected]");
message.setSeverity(FacesMessage.SEVERITY_ERROR);
throw new ValidatorException(message);
}
}
}
有這個代碼默認模式。但是,我試圖用另一種模式替換它。 (?:(?:(?:[^ @,「[] \ x5c \ x00- \ x20 \ x7f- \ xff。] | \ x5c(?= [@,」[ ](?:[^ @,「[] \ x5c \ x00- \ x20 \ x7f- \ xff。] |(?< = \ x5c)[@ 「[] \ x5c \ x00- \ X20 \ x7f- \ XFF] | \ x5c(?= [@,」[] \ x5c \ x00- \ X20 \ x7f- \ XFF])|。?(= [^ 。])){1,62}(?:[^ @,「[] \ x5c \ x00- \ x20 \ x7f- \ xff。] |(?< = \ x5c)[@,」[] \ x5c \ x00- \ X20 \ x7f- \ XFF])| [^ @, 「[] \ x5c \ x00- \ X20 \ x7f- \ XFF] {1,2})|。」(?:[^「] |( (α:[α-ZA-Z0-9] [a-zA-Z0-9-] { 1,61} [A-ZA-Z0-9] | [A-ZA-Z0-9])+(?: XN - [A-ZA-Z0-9] + |。?。?。[A- ZA-Z] {2,6-})| [(:[0-1?] \ d \ d | 2 [0-4] \ d | 25 [0-5])(:???(?: [0-1]?\ d?\ d | 2 [0-4] \ d | 25 [0-5])){3}])$/
我知道可以使用這種模式嗎?或者我需要把一些括號等,使其工作?我現在收到非法開始的表達錯誤。
你在使用什麼模式? – 8vius