PHP regular expression common character cluster

xiaoxiao2021-03-06  64

When Internet programming is programmed, it is often necessary to check if a string "legal", such as QQ must be composed of numbers, Email's format is correct, implement such verification, if each character is checked, or Write each character and separator to form a regular expression, apparently not a good way. PHP provides a relatively convenient way to define a character set, this collection, generally referred to as a character cluster, generally in brackets [] enclose characters.

First, we start from the simplest example. [ABDE] This expression containing square brackets defines a character cluster while it is also a regular expression. It means that as long as there is any string contains any one in characters A, B, D, E, this string is the matching character cluster [abde]. Here is an example ";} else {echo "$ str1 does not match [abde]
";} if (EREG ("[ABDE]", "$ STR2")) {echo "$ STR2 matches [abde] ";} else {echo" $ str2 does not match [ABDE] ";}?>

The results we got will be: Abmkaq2334FL match [abde] cffghki9-0 does not match [ABDE]

Here are some common character clusters [AZ] matching from AZ, that is, match all lowercase letters [AZ] matching from AZ letters, that is, match all uppercase letters, therefore [A-ZA-Z] is matched all Letters here only a simple [AZ] example ";} else { Echo "$ str1 does not match $ pattern
";} if (EREG ("$ pattern", "$ str2"))) {echo "$ str2 matches $ pattern";} else {echo "$ str2 does not match $ PATTERN ";}?> The result we get will be ABQ2334FL mismatch [AZ] {5,} Cffghki9-0 match [AZ] {5,}

Similarly, [0-9] means matching all numbers ";} else {echo "$ str1 does not match $ Pattern
";}?> The result will be ABQ2334FL mismatch [0-9] {5,}

In addition, [NTRF] matches all empty characters, including newline characters, Tab, Enter (excluding spaces) In square brackets, using characters "^" to take not, ie, the following characters, Then do not match, otherwise it is matched. [^ AZ] Match all non-written letters [^ AZ], [^ 0-9] means the same, match all non-uppercase letters, match all non-digital characters. ";} else {echo" $ str1 does not match $ pattern
";}?> The result will be: AF does not match [^ AZ], string "AFS555 ^^ _" will be matched because it contains non-lowercase characters. It should be noted here that ^ is not the same in []. ^ [AF] refers to six of AF The character string matching mode of the beginning in the letter, [^ AF] is the character in the matching string is not AF and other six letters, and there is such a regular expression ^ [^ AF], if a string is not AF The string starting will be matched.

In addition, special characters ".", Match all characters (. ) Other than the new row can match any strings that divide the empty string and a new line characters.

In addition to the above character clusters, PHP also provides a number of built-in general character clusters: [[: alpha:]] any letter [[: Digit:]] Any number [[: alnum:]] Any number and letters [[: Space:] Any blank character [[: Upper:]] Any big character letters [[: Lower:]] Any lowercase letters [[: punct:]] Any punctuation [[: xdigit:]] any hexadecimal number [[: cntrl:]] Any character of any ASCII value of less than 32

Special, [". CHR (0xA1)." - ". Chr (0xff)."] Match all Chinese characters, the following is an example

The value of $ STR is replaced into the "Yifeng Network", the result will be that this is a pure Chinese string

转载请注明原文地址:https://www.9cbs.com/read-85757.html

New Post(0)