Introduction to delimiters and atoms in PHP regular expressions_PHP Tutorial

WBOY
Release: 2016-07-21 15:15:38
Original
996 people have browsed it

本节内容我们将介绍PHP中正则 表达式的基础语法:定界符和原子。内容包含了定界符的定义以及原子的定义和构成等等。其中原子的构成十分灵活,以便满足我们对处理字符串的需求。在这之 前,我们需要先了解一个正则表达式处理函数preg_match()来进行测试,以方便我们教程示例的进行。

先来看一下正则表达式的定界符、正则表达式的构成以及preg_match()函数:

1,正则表达式的定界符。

除了字母、数字和反斜线\以外的任何字符都可以为定界符号,比如 | |、//、{}、!!等等,但是需要注意,如果没有特殊需要,我们都使用正斜线//作为正则表达式的定界符号。

2,正则表达式的构成。

我们看一下这个公式:/原子和元字符/模式修正符

也就是说,正则表达式的原子和元字符都放在定界符之间,而模式修正符放在定界符之外。

3,preg_match()函数

我们会在后面进行详细解释,这里只是为了帮助测试,其返回一个布尔值,表示是否成功匹配。

了解完以上简单的内容,让我们进入正题。

正则表达式中的原子

什么是原子?原子是正则表达式的最基本组成单位,而且必须至少要包含一个原子。只要一个正则表达式可以单独使用的字符,就是原子。

这个概念可能看起来很模糊,没关系,下面我们来介绍一下正则表达式中原子的构成方式。

原子构成方式
1,所有打印(所有可以在屏幕上输出的字符串)和非打印字符(看不到的,比如空格,换行符等等)

2,如果所有有意义的字符,想做为原子使用,统统使用“\”转义字符进行转义即可。如:\. \* \+ \? \( \<\>。

注意:" \ "转义字符可以将有意义的字符转成没意义的字符,还可以将没意义的字符转为有意义的字符。如:\d表示任意一个十进制的数字。

3,在正则表达式中可以直接使用一些系统提供的代表范围的原子,如下面的表格所示:

代表范围的原子  说明  自定义原子表示法
 \d  表示任意一个十进制的数字  [0-9]
 \D  表示任意一个除数字这外的字符  [^0-9]
 \s  表示任意一个空白字符,空格、\n\r\t\f  [\n\r\t\f ]
 \S  表示任意一个非空白  [^\n\r\t\f ]
 \w  表示任意一个字 a-zA-Z0-9_  [a-zA-Z0-9_]
 \W  表示任意一个非字,除了a-zA-Z0-9_以外的任意一个字符  [^a-zA-Z0-9_]

4. Customize the atom table (using square brackets []), which can match any atom in the square brackets.

In the above table, we have equivalently converted the range atoms provided by the system in a custom way. Since the system cannot provide all the atoms I need, it is necessary to customize the atom table. For example, if we want to match letters or numbers, we need to write the atoms as [a-zA-Z0-9].

Note here:

A, the symbol "-" represents the range, such as [a-z] represents the lowercase letters a to z, but never write it as [a- 9] This form!

B, the symbol "^" means negation, and must be placed at the beginning of the square brackets. For example, if we want to match non-digits, the atom is [^0-9].

Let’s take a look at an example of using regular expression atoms. The code is as follows:

Copy the code The code is as follows:

$pattern = '/d/';//Number atomic table, which is the pattern of regular expressions
$string = 'dsadsadsa';//Requires matching String
if(preg_match($pattern, $string)){
echo "Regular expression{$pattern} and string{$string} Match successful";
}else{
echo "Regular expression {$pattern} and string {$string} failed to match< ;/span>";
}
?>


Note: If one atom in the custom atom table is matched by a string, the match is successful. Removing the square brackets of the custom atom table means matching the entire string. For example, '/abc/' means that the substring abc must be included in the string to be matched, and '/[abc]/' means that the string will be matched as long as it contains any one of the characters a, b, and c.

You can modify the pattern in the above example (that is, the pattern variable $pattern of the regular expression), and then verify the atoms of the regular expression we talk about in this section.

This section has finished introducing the delimiters and atoms of regular expressions. I believe that based on practice, you will already be able to use atoms of regular expressions. In the next section we will introduce metacharacters in PHP regular expressions, don’t miss it.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/326083.htmlTechArticleIn this section we will introduce the basic syntax of regular expressions in PHP: delimiters and atoms. The content includes the definition of delimiters, the definition and composition of atoms, etc. Among them, the composition of atoms is ten...
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!