How do I tell a regular expression a match of at least x numeric alphanumeric?

I have a form in which a user submits a field. A field can have letters, numbers, and punctuation. But I want to check that at least 3 characters are letters. How can I specify a regex?

For instance,

$string = "ab'c";

And I need something like

if (preg_match("/[a-z]{3}/i", $string))
    print "true";
else
    print "false";

This line has three letters, although it has an apostrophe. That must be true. But for some reason this is checking the lie right now.

Any help?

+3
source share
4 answers

How about case insensitivity:

([a-z][^a-z]*){3}

Searches for 3 groups of letters and any number of letters.

+3
source

, " x ". ,

preg_match_all('~([a-z][^a-z]*){3}~', "ab'c")

- , ( preg_replace):

preg_replace('~[a-z]~', '', "ab'c", -1, $count);
print_r($count); // prints "3"
+1

:

^([0-9,]*[A-Za-z]){3}[A-Za-z0-9,]*$

:

if (strlen(preg_replace('/[^A-Za-z]+/', '', $str)) >= 3) {
    // $str contains at least three letters
}
+1

: $ ;)

if (preg_match("/[a-zA-Z\']{3}$/i", $string))
    print "true";
else
    print "false"; 

: , . :
^([a-zA-Z\']{3,}(.?))$

:
hell'o < - true
h31l0 < - false hello < - true

0

Source: https://habr.com/ru/post/1736799/


All Articles