How do I tell regex to match at least x number of alphanumeric?
I have form where user submits field. Field can have letters, numbers, and punctuation. But I want to check to make sure that at least 3 of the characters are letters. How can I regex that?
For example,
$string = "ab'c";
And I need something like,
if (pr开发者_JAVA百科eg_match("/[a-z]{3}/i", $string))
print "true";
else
print "false";
That string has three letters, even though it has an apostrophe. It should test true. But for some reason, that tests false right now.
Any help?
How about a case insensitive match on:
([a-z][^a-z]*){3}
Looks for 3 groups of a letter, and any number of non letters.
You cannot write a regexp that checks for "at least x symbols of a class". Of course you can
preg_match_all('~([a-z][^a-z]*){3}~', "ab'c")
In more complex cases, you can replace the class to something else and then compare results (or simply use preg_replace fourth parameter):
preg_replace('~[a-z]~', '', "ab'c", -1, $count);
print_r($count); // prints "3"
Try this regular expression:
^([0-9,]*[A-Za-z]){3}[A-Za-z0-9,]*$
You could also remove all non-letter characters and check the length:
if (strlen(preg_replace('/[^A-Za-z]+/', '', $str)) >= 3) {
// $str contains at least three letters
}
Try this:
The $
matches the end of the string ;)
if (preg_match("/[a-zA-Z\']{3}$/i", $string))
print "true";
else
print "false";
Edit:
Sorry, I misunderstood your question. Try this:
^([a-zA-Z\']{3,}(.?))$
Results:
hell'o
<-- true
h31l0
<-- false
hello
<-- true
精彩评论