Does anyone know how to inverse a regular expression?
For example, this email validation regex, how do I return the opposite of it’s match? I don’t want to simply add a ! in front of the preg_match either.
$REG_EXP_URL = '/(^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$)/';
$email = 'some@email.com'; //Should not match. e.g. some@email should match
if(preg_match($REG_EXP_URL, $email)){
//Didn't match email.
}
else{
//Matched email
}
why would you not just add ! thats what it’s there for
$REG_EXP_URL = '/(^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$)/';
$email = 'some@email.com'; //Should not match. e.g. some@email should match
if(!preg_match($REG_EXP_URL, $email)){
//Didn't match email.}
else{
//Matched email
}
yeah, that would be easy, but I want to use that same regex in a uniform manner with other regular expressions that all return the negated match.
$otherRegex = '/[^\\d]/';
$otherRegex = '/[^a-z]/';
I have a dynamic system going on here. And no, they are not simple like those above.