What is the PHP regular expression character set? How can the use of PHP regular expression character sets help our development and use? What are the forms and usage rules of the PHP regular expression character set? Let’s introduce one by one,
Related concepts of PHP regular expression character set:
In INTERNET programs, regular expressions are usually used to verify user input. When a user submits a FORM, it is not enough to use ordinary literal characters to determine whether the entered phone number, address, email address, credit card number, etc. are valid. So we need to use a more free way to describe the pattern we want, which is character clusters. To create a cluster representing all vowel characters, place all vowel characters in square brackets.
PHP regular expression character set format:
<ol class="dp-c"><li class="alt"><span><span>[AaEeIiOoUu] </span></span></li></ol>
This pattern matches any vowel character, but can only represent one character. Hyphens can be used to represent a range of characters, such as:
<ol class="dp-c"> <li class="alt"><span><span>[a-z] </span><span class="comment">//匹配所有的小写字母 </span><span> </span></span></li> <li> <span>[A-Z] </span><span class="comment">//匹配所有的大写字母 </span><span> </span> </li> <li class="alt"> <span>[a-zA-Z] </span><span class="comment">//匹配所有的字母 </span><span> </span> </li> <li> <span>[0-9] </span><span class="comment">//匹配所有的数字 </span><span> </span> </li> <li class="alt"> <span>[0-9.-] </span><span class="comment">//匹配所有的数字,句号和减号 </span><span> </span> </li> <li> <span>[ frtn] </span><span class="comment">//匹配所有的白字符 </span><span> </span> </li> </ol>
Likewise, these only represent one character, which is very important. If you want to match a string consisting of a lowercase letter and a digit, such as "z2", "t6" or "g7", but not "ab2", "r2d3" or "b52", use this pattern:
<ol class="dp-c"><li class="alt"><span><span>^[a-z][0-9]$ </span></span></li></ol>
Although [a-z] represents a range of 26 letters, here it can only match strings where the first character is a lowercase letter.
Usage of PHP regular expression character set:
It was mentioned earlier that ^ represents the beginning of a string, but it has another meaning. When ^ is used within a set of square brackets, it means "not" or "exclude" and is often used to eliminate a certain character. Using the previous example, we require that the first character cannot be a number:
<ol class="dp-c"><li class="alt"><span><span>^[^0-9][0-9]$ </span></span></li></ol>
This pattern matches "&5", "g7" and "-2", but does not match "12", "66" does not match. Here are a few examples of excluding specific characters:
<ol class="dp-c"> <li class="alt"><span><span>[^a-z] </span><span class="comment">//除了小写字母以外的所有字符 </span><span> </span></span></li> <li> <span>[^/^] </span><span class="comment">//除了"/"和"^"字符之外的所有字符 </span><span> </span> </li> <li class="alt"> <span>[^</span><span class="string">"'] //除了双引号("</span><span>)和单引号(')之外的所有字符 </span> </li> </ol>
The special characters "." (dot, period) are used in regular expressions to represent all characters except "new line". So the pattern "^.5$" matches any two-character string that ends with the number 5 and begins with some other non-"newline" character. The pattern "." can match any string, except empty strings and strings containing only a "new line".
This concludes the introduction to the PHP regular expression character set. I hope it will be helpful for you to understand and use the PHP regular expression character set.