Home Backend Development PHP Tutorial PHP正则表达式之定界符和原子介绍_PHP

PHP正则表达式之定界符和原子介绍_PHP

Jun 01, 2016 pm 12:08 PM
regular expression

正则表达式

本节内容我们将介绍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,自定义原子表(使用方括号[]),可以匹配方括号中的任何1个原子。

在上面的表格中我们已经将系统提供的范围原子使用自定义的方式作了等价转换。由于系统不可能提供所有我需要的原子,所以自定义原子表就显得十分必要了,比如我们想要匹配字母或者数字,就需要将原子写成[a-zA-Z0-9]。

这里需要注意:

A,符号“-”表示范围,如[a-z]表示小写字母a到z,但千万不要写成[a-9]这种形式!

B, 符号“^”表示取反,一定要放在方括号的开头,比如我们想要匹配非数字,则原子为[^0-9]。

下面我们来看一下正则表达式原子的使用实例,代码如下:

复制代码 代码如下:
$pattern = '/\d/';//数字原子表,也就是正则表达式的模式
$string = 'dsadsadsa';//需要匹配的字符串
if(preg_match($pattern, $string)){
echo "正则表达式{$pattern} 和字符串 {$string} 匹配成功";
}else{
echo "正则表达式{$pattern}和字符串{$string}匹配失败";
}
?>


注意:自定义原子表中的原子有一个被字符串匹配上,就匹配成功了。而去掉自定义原子表的方括号,则表示匹配整个字符串。如'/abc/'表示字符串中必须有abc这个子串才能被匹配,而'/[abc]/'表示字符串中只要包含a、b和c中的任何一个字符,即被匹配。

大家可以将上面实例中的模式进行修改(也就是正则表达式的模式变量$pattern),进而对我们本节所讲的正则表达式的原子进行验证。

本节关于正则表达式的定界符和原子就介绍完了,相信在练习的基础上,你已经会使用正则表达式的原子了。下节我们将介绍php正则表达式中的元字符,不要错过啊。
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP regular expression validation: number format detection PHP regular expression validation: number format detection Mar 21, 2024 am 09:45 AM

PHP regular expression verification: Number format detection When writing PHP programs, it is often necessary to verify the data entered by the user. One of the common verifications is to check whether the data conforms to the specified number format. In PHP, you can use regular expressions to achieve this kind of validation. This article will introduce how to use PHP regular expressions to verify number formats and provide specific code examples. First, let’s look at common number format validation requirements: Integers: only contain numbers 0-9, can start with a plus or minus sign, and do not contain decimal points. floating point

How to validate email address in Golang using regular expression? How to validate email address in Golang using regular expression? May 31, 2024 pm 01:04 PM

To validate email addresses in Golang using regular expressions, follow these steps: Use regexp.MustCompile to create a regular expression pattern that matches valid email address formats. Use the MatchString function to check whether a string matches a pattern. This pattern covers most valid email address formats, including: Local usernames can contain letters, numbers, and special characters: !.#$%&'*+/=?^_{|}~-`Domain names must contain at least One letter, followed by letters, numbers, or hyphens. The top-level domain (TLD) cannot be longer than 63 characters.

How to match timestamps using regular expressions in Go? How to match timestamps using regular expressions in Go? Jun 02, 2024 am 09:00 AM

In Go, you can use regular expressions to match timestamps: compile a regular expression string, such as the one used to match ISO8601 timestamps: ^\d{4}-\d{2}-\d{2}T \d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-][0-9]{2}:[0-9]{2})$ . Use the regexp.MatchString function to check if a string matches a regular expression.

Master regular expressions and string processing in Go language Master regular expressions and string processing in Go language Nov 30, 2023 am 09:54 AM

As a modern programming language, Go language provides powerful regular expressions and string processing functions, allowing developers to process string data more efficiently. It is very important for developers to master regular expressions and string processing in Go language. This article will introduce in detail the basic concepts and usage of regular expressions in Go language, and how to use Go language to process strings. 1. Regular expressions Regular expressions are a tool used to describe string patterns. They can easily implement operations such as string matching, search, and replacement.

PHP regular expressions: exact matching and exclusion of fuzzy inclusions PHP regular expressions: exact matching and exclusion of fuzzy inclusions Feb 28, 2024 pm 01:03 PM

PHP Regular Expressions: Exact Matching and Exclusion Fuzzy inclusion regular expressions are a powerful text matching tool that can help programmers perform efficient search, replacement and filtering when processing text. In PHP, regular expressions are also widely used in string processing and data matching. This article will focus on how to perform exact matching and exclude fuzzy inclusion operations in PHP, and will illustrate it with specific code examples. Exact match Exact match means matching only strings that meet the exact condition, not any variations or extra words.

How to verify password using regular expression in Go? How to verify password using regular expression in Go? Jun 02, 2024 pm 07:31 PM

The method of using regular expressions to verify passwords in Go is as follows: Define a regular expression pattern that meets the minimum password requirements: at least 8 characters, including lowercase letters, uppercase letters, numbers, and special characters. Compile regular expression patterns using the MustCompile function from the regexp package. Use the MatchString method to test whether the input string matches a regular expression pattern.

How to detect URL with regular expression in Golang? How to detect URL with regular expression in Golang? May 31, 2024 am 10:32 AM

The steps to detect URLs in Golang using regular expressions are as follows: Compile the regular expression pattern using regexp.MustCompile(pattern). Pattern needs to match protocol, hostname, port (optional), path (optional) and query parameters (optional). Use regexp.MatchString(pattern,url) to detect whether the URL matches the pattern.

What are the regular expression wildcards? What are the regular expression wildcards? Nov 17, 2023 pm 01:40 PM

Regular expression wildcards include ".", "*", "+", "?", "^", "$", "[]", "[^]", "[a-z]", "[A-Z] ","[0-9]","\d","\D","\w","\W","\s&quo

See all articles