Home > Backend Development > PHP Tutorial > Detailed explanation of the use of regular expressions (1)_PHP tutorial

Detailed explanation of the use of regular expressions (1)_PHP tutorial

WBOY
Release: 2016-07-13 17:20:14
Original
809 people have browsed it

  如果我们问那些UNIX系统的爱好者他们最喜欢什么,答案除了稳定的系统和可以远程启动之外,十有八九的人会提到正则表达式;如果我们再问他们最头痛的是什么,可能除了复杂的进程控制和安装过程之外,还会是正则表达式。那么正则表达式到底是什么?如何才能真正的掌握正则表达式并正确的加以灵活运用?本文将就此展开介绍,希望能够对那些渴望了解和掌握正则表达式的读者有所助益。

入门简介

  简单的说,正则表达式是一种可以用于模式匹配和替换的强有力的工具。我们可以在几乎所有的基于UNIX系统的工具中找到正则表达式的身影,例如,vi编辑器,Perl或PHP脚本语言,以及awk或sed shell程序等。此外,象JavaScript/" target="_blank">JavaScript这种客户端的脚本语言也提供了对正则表达式的支持。由此可见,正则表达式已经超出了某种语言或某个系统的局限,成为人们广为接受的概念和功能。

  正则表达式可以让用户通过使用一系列的特殊字符构建匹配模式,然后把匹配模式与数据文件、程序输入以及WEB页面的表单输入等目标对象进行比较,根据比较对象中是否包含匹配模式,执行相应的程序。

  举例来说,正则表达式的一个最为普遍的应用就是用于验证用户在线输入的邮件地址的格式是否正确。如果通过正则表达式验证用户邮件地址的格式正确,用户所填写的表单信息将会被正常处理;反之,如果用户输入的邮件地址与正则表达的模式不匹配,将会弹出提示信息,要求用户重新输入正确的邮件地址。由此可见正则表达式在WEB应用的逻辑判断中具有举足轻重的作用。

基本语法

  在对正则表达式的功能和作用有了初步的了解之后,我们就来具体看一下正则表达式的语法格式。

  正则表达式的形式一般如下:

  /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+/

The above regular expression can be used to match one or more space characters in the target object.

 /d000/

If we have a complex financial statement, we can easily find all the amounts totaling thousands of dollars through the above regular expression.

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.

The more commonly used locators include: “^”, “$”, “ ” 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 "$" locator specifies that the matching pattern must appear at the beginning or end of the target string. One of the two boundaries, and 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 can neither be the beginning of the target string nor the target string. at the end. Similarly, we can also think of "^" and "$" and " " and "B" as two sets of locators that are inverse operations. For example:

.

 /^hell/

Since the above regular expression contains the "^" locator, it can match strings starting with "hell", "hello" or "hellhound" in the target object.

 /ar$/

Since the above regular expression contains the "$" locator, it can match strings ending with "car", "bar" or "ar" in the target object.

 / bom/

Since the above regular expression pattern starts with the " " locator, it can match the string starting with "bomb", or "bom" in the target object.

 /man /

Since the above regular expression pattern ends with the " " locator, it can match strings ending with "human", "woman" or "man" in the target object.

In order to facilitate users to set matching patterns more flexibly, regular expressions allow users to specify a certain 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". 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 characters in the target object except A, B, and C. Generally speaking, when "^" appears inside "[]", it is regarded as a negation 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.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/532606.htmlTechArticleIf we ask those UNIX system enthusiasts what they like most, the answer is besides a stable system and the ability to start remotely In addition, nine out of ten people will mention regular expressions; if we then...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template