Home php教程 php手册 [转译]正则表达式的基本语法(一)

[转译]正则表达式的基本语法(一)

Jun 21, 2016 am 09:13 AM
notice quot the

语法|正则

正则表达式的基本语法
首先让我们看两个特殊的符号'^'和'$'。他们的作用是分别指出一个字符串的开始和结束。例子如下:

"^The":表示所有以"The"开始的字符串("There","The cat"等);
"of despair$":表示所以以"of despair"结尾的字符串;
"^abc$":表示开始和结尾都是"abc"的字符串——呵呵,只有"abc"自己了;
"notice":表示任何包含"notice"的字符串。

象最后那个例子,如果你不使用两个特殊字符,你就在表示要查找的串在被查找串的任意部分——你并不把它定位在某一个顶端。

其它还有'*','+'和'?'这三个符号,表示一个或一序列字符重复出现的次数。它们分别表示“没有或更多”,“一次或更多”还有“没有或一次”。下面是几个例子:

"ab*":表示一个字符串有一个a后面跟着零个或若干个b。("a", "ab", "abbb",……);
"ab+":表示一个字符串有一个a后面跟着至少一个b或者更多;
"ab?":表示一个字符串有一个a后面跟着零个或者一个b;
"a?b+$":表示在字符串的末尾有零个或一个a跟着一个或几个b。

你也可以使用范围,用大括号括起,用以表示重复次数的范围。

"ab{2}":表示一个字符串有一个a跟着2个b("abb");
"ab{2,}":表示一个字符串有一个a跟着至少2个b;
"ab{3,5}":表示一个字符串有一个a跟着3到5个b。

请注意,你必须指定范围的下限(如:"{0,2}"而不是"{,2}")。还有,你可能注意到了,'*','+'和'?'相当于"{0,}","{1,}"和"{0,1}"。



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

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)

After 2 months, the humanoid robot Walker S can fold clothes After 2 months, the humanoid robot Walker S can fold clothes Apr 03, 2024 am 08:01 AM

Editor of Machine Power Report: Wu Xin The domestic version of the humanoid robot + large model team completed the operation task of complex flexible materials such as folding clothes for the first time. With the unveiling of Figure01, which integrates OpenAI's multi-modal large model, the related progress of domestic peers has been attracting attention. Just yesterday, UBTECH, China's "number one humanoid robot stock", released the first demo of the humanoid robot WalkerS that is deeply integrated with Baidu Wenxin's large model, showing some interesting new features. Now, WalkerS, blessed by Baidu Wenxin’s large model capabilities, looks like this. Like Figure01, WalkerS does not move around, but stands behind a desk to complete a series of tasks. It can follow human commands and fold clothes

PHP Notice: Use of undefined constant solution PHP Notice: Use of undefined constant solution Jun 24, 2023 pm 11:15 PM

PHPNotice: Useofundefinedconstant Solution During the PHP development process, you may encounter the error message "PHPNotice: Useofundefinedconstant". This is usually due to the use of undefined constants in the code. Constants should be defined first before they can be referenced correctly. If they are not defined or are spelled incorrectly, the above error message will appear. Here are some ways to solve this problem

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

How to set php not to prompt notice How to set php not to prompt notice Feb 21, 2023 am 09:12 AM

How to set php not to prompt notice: 1. Change "error_reporting" in the "php.ini" file to "error_reporting = E_ALL & ~E_NOTICE"; 2. Add the code to the specified page as "error_reporting(E_ALL ^ ​​E_NOTICE);".

PHP Notice: Undefined offset: 0 solution PHP Notice: Undefined offset: 0 solution Jun 22, 2023 am 08:12 AM

In PHP development, sometimes when we run the code, the prompt "PHPNotice: Undefinedoffset:0" will appear. This prompt is usually accompanied by an array out-of-bounds error. When the array subscript exceeds the specified range, PHP will set its value to NULL by default and give a notification message. Although this prompt message does not affect the execution of the program, it will affect the running efficiency of the program and the readability of the code. So, what should we do when encountering this situation? Here are some solutions

PHP Notice: Undefined variable: solution in solution PHP Notice: Undefined variable: solution in solution Jun 22, 2023 pm 10:01 PM

For PHP developers, encountering the "Undefinedvariable" warning message is a very common thing. This situation usually occurs when trying to use undefined variables, which will cause the PHP script to fail to execute normally. This article will introduce how to solve the problem of "PHPNotice:Undefinedvariable: in solution". Cause of the problem: In a PHP script, if a variable is not initialized or assigned a value, "Un

PHP Notice: Undefined offset solution PHP Notice: Undefined offset solution Jun 25, 2023 am 09:51 AM

PHPNotice:Undefinedoffset is a common PHP program error. It means that the program attempts to use a subscript that does not exist in the array, causing the program to fail to run normally. This error usually occurs when the PHP interpreter displays the following warning message: Notice: Undefinedoffset. Here are some ways to resolve the PHPNotice:Undefinedoffset error: Check the code First, it should

What currency is THE? Is THE coin worth investing in? What currency is THE? Is THE coin worth investing in? Feb 21, 2024 pm 03:49 PM

What currency is THE? THE (Tokenized Healthcare Ecosystem) is a digital currency that uses blockchain technology to focus on innovation and reform in the healthcare industry. THE coin's mission is to use blockchain technology to improve the efficiency and transparency of the medical industry and promote more efficient cooperation among all parties, including patients, medical staff, pharmaceutical companies and medical institutions. The Value and Characteristics of THE Coin First of all, THE Coin, as a digital currency, has the advantages of blockchain - decentralization, high security, transparent transactions, etc., allowing participants to trust and rely on this system. Secondly, the uniqueness of THE coin is that it focuses on the medical and health industry, using blockchain technology to transform the traditional medical system and improve

See all articles