Reputation: 81
"Hello" returns true
"12345" returns true
"Hello1" returns false
"123H" returns false
regex can possible check only letters and numbers except both?
or
Check function of PHP?
Upvotes: 0
Views: 54
Reputation: 67968
^(?=(?:\d+|[a-zA-Z]+)$)[a-zA-Z0-9]+$
Another variation.See demo.
https://regex101.com/r/sH8aR8/11
$re = "/^(?=(?:\\d+|[a-zA-Z]+)$)[a-zA-Z0-9]+$/m";
$str = "Hello\n12345\nHello1\n123H";
preg_match_all($re, $str, $matches);
Upvotes: 1
Reputation: 52185
You could use something like so: ^([A-Za-z]+)|([0-9]+)$
. This will make sure that the string is either full of letters exclusively, or numbers exclusively, but not both.
You can check the regular expression here.
Upvotes: 2
Reputation: 26753
Regex:
/^[A-za-z]*|[0-9]*$/
Check for start of line, then any numbers of letter OR any number of numbers, then end of line.
A blank line will return true. If that's a problem change the *
to a +
.
Upvotes: 2