Home >Backend Development >PHP Tutorial >In-depth study and discussion of PHP regular expressions_PHP tutorial
1、入门简介
简单的说,正则表达式是一种可以用于模式匹配和替换的强有力的工具。我们可以在几乎所有的基于UNIX系统的工具中找到正则表达式的身影,例如,vi编辑器,Perl或PHP脚本语言,以及awk或sed shell程序等。此外,象JavaScript这种客户端的脚本语言也提供了对正则表达式的支持。由此可见,正则表达式已经超出了某种语言或某个系统的局限,成为人们广为接受的概念和功能。
正则表达式可以让用户通过使用一系列的特殊字符构建匹配模式,然后把匹配模式与数据文件、程序输入以及WEB页面的表单输入等目标对象进行比较,根据比较对象中是否包含匹配模式,执行相应的程序。
举例来说,正则表达式的一个最为普遍的应用就是用于验证用户在线输入的邮件地址的格式是否正确。如果通过正则表达式验证用户邮件地址的格式正确,用户所填写的表单信息将会被正常处理;反之,如果用户输入的邮件地址与正则表达的模式不匹配,将会弹出提示信息,要求用户重新输入正确的邮件地址。由此可见正则表达式在WEB应用的逻辑判断中具有举足轻重的作用。
2、基本语法
在对正则表达式的功能和作用有了初步的了解之后,我们就来具体看一下正则表达式的语法格式。
正则表达式的形式一般如下:
/love/
其中位于“/”定界符之间的部分就是将要在目标对象中进行匹配的模式。用户只要把希望查找匹配对象的模式内容放入“/”定界符之间即可。为了能够使用户更加灵活的定制模式内容,正则表达式提供了专门的“元字符”。所谓元字符就是指那些在正则表达式中具有特殊意义的专用字符,可以用来规定其前导字符(即位于元字符前面的字符)在目标对象中的出现模式。
较为常用的元字符包括: “+”, “*”,以及 “?”。其中,“+”元字符规定其前导字符必须在目标对象中连续出现一次或多次,“*”元字符规定其前导字符必须在目标对象中出现零次或连续多次,而“?”元字符规定其前导对象必须在目标对象中连续出现零次或一次。
下面,就让我们来看一下正则表达式元字符的具体应用。
/fo+/
因为上述正则表达式中包含“+”元字符,表示可以与目标对象中的 “fool”, “fo”, 或者 “football”等在字母f后面连续出现一个或多个字母o的字符串相匹配。
/eg*/
因为上述正则表达式中包含“*”元字符,表示可以与目标对象中的 “easy”, “ego”, 或者 “egg”等在字母e后面连续出现零个或多个字母g的字符串相匹配。
/Wil?/
因为上述正则表达式中包含“?”元字符,表示可以与目标对象中的 “Win”, 或者 “Wilson”,等在字母i后面连续出现零个或一个字母l的字符串相匹配。
除了元字符之外,用户还可以精确指定模式在匹配对象中出现的频率。例如,
/jim{2,6}/
上述正则表达式规定字符m可以在匹配对象中连续出现2-6次,因此,上述正则表达式可以同jimmy或jimmmmmy等字符串相匹配。
在对如何使用正则表达式有了初步了解之后,我们来看一下其它几个重要的元字符的使用方式。
\s:用于匹配单个空格符,包括tab键和换行符;
\S:用于匹配除单个空格符之外的所有字符;
\d:用于匹配从0到9的数字;
\w:用于匹配字母,数字或下划线字符;
\W:用于匹配所有与\w不匹配的字符;
. :用于匹配除换行符之外的所有字符。
(说明:我们可以把\s和\S以及\w和\W看作互为逆运算)
下面,我们就通过实例看一下如何在正则表达式中使用上述元字符。
/\s+/
上述正则表达式可以用于匹配目标对象中的一个或多个空格字符。
/\d000/
如果我们手中有一份复杂的财务报表,那么我们可以通过上述正则表达式轻而易举的查找到所有总额达千元的款项。
In addition to the metacharacters we introduced above, regular expressions also have another unique special character, namely the locator. Locators are used to specify where the matching pattern appears in the target object.
Commonly used locators include: "^", "$", "b" and "B". Among them, the "^" locator specifies that the matching pattern must appear at the beginning of the target string, the "$" locator specifies that the matching pattern must appear at the end of the target object, and the b locator specifies that the matching pattern must appear at the beginning or end of the target string. One of the two boundaries at the end, while the "B" locator stipulates that the matching object must be located within the two boundaries of the beginning and end of the target string, that is, the matching object cannot be used as the beginning of the target string or as a target character. The end of the string. Similarly, we can also regard "^" and "$" and "b" and "B" as two sets of locators that are inverse operations of each other. For example:
/^hell/
Because the above regular expression contains the "^" locator, it can be used with the target object with "hell", "hello" or "hellhound" Matches strings starting with ".
/ar$/
Because the above regular expression contains the "$" locator, it can be matched with characters ending in "car", "bar" or "ar" in the target object String matches.
/bbom/
Because the above regular expression pattern starts with the "b" locator, it can match the string starting with "bomb", or "bom" in the target object .
/manb/
Because the above regular expression pattern ends with the "b" locator, it can be matched with characters in the target object that end with "human", "woman" or "man" String matches.
In order to make it easier for users to set matching patterns more flexibly, regular expressions allow users to specify a range in the matching pattern without being limited to specific characters. For example:
/[A-Z]/
The above regular expression will match any uppercase letter from A to Z.
/[a-z]/
The above regular expression will match any lowercase letter in the range from a to z.
/[0-9]/
The above regular expression will match any number from 0 to 9.
/([a-z][A-Z][0-9])+/
The above regular expression will match any string consisting of letters and numbers, such as "aB0" etc. match. One thing that users need to pay attention to here is that you can use "()" in regular expressions to combine strings together. The content contained in the "()" symbol must also appear in the target object. Therefore, the above regular expression will not match a string such as "abc" because the last character in "abc" is a letter and not a number.
If we want to implement a regular expression similar to the "OR" operation in programming logic and select any one of multiple different patterns for matching, we can use the pipe character "|". For example:
/to|too|2/
The above regular expression will match "to", "too", or "2" in the target object.
There is also a more commonly used operator in regular expressions, which is the negation operator "[^]". Different from the locator "^" we introduced earlier, the negation character "[^]" specifies that the string specified in the pattern cannot exist in the target object. For example:
/[^A-C]/
The above string will match any character except A, B, and C in the target object. Generally speaking, when "^" appears inside "[]", it is regarded as a negative operator; when "^" is located outside "[]", or there is no "[]", it should be regarded as a negative operator. locator.
Finally, when users need to add metacharacters to the regular expression pattern and find their matching objects, they can use the escape character "". For example:
/Th*/
The above regular expression will match "Th*" instead of "The" in the target object.
3. Usage examples
① You can use the ereg() function in PHP to perform pattern matching operations. The usage format of the ereg() function is as follows:
The following is the quoted content:
ereg(pattern, string)
where pattern represents the pattern of the regular expression, and string is The target object to perform the find and replace operation on. The same is to verify the email address. The program code written in PHP is as follows:
We can use JavaScript to write the following script to verify the validity of the email address entered by the user. < head>