PHP中的正则表达式_PHP
正则表达式
综述正则表达式是一种可以用于模式匹配和替换的强有力的工具。正则表达式可以让用户通过使用一系列的特殊字符构建匹配模式,然后把匹配模式与数据文件、程序输入以及WEB页面的表单输入等目标对象进行比较,根据比较对象中是否包含匹配模式,执行相应的程序。
如何使用基本模式匹配?
模式,是正规表达式最基本的元素,它们是一组描述字符串特征的字符。模式可以很简单,由普通的字符串组成,也可以非常复杂,往往用特殊的字符表示一个范围内的字符重复出现,或表示上下文。我们先看一些正则表达式中的特殊字符。
特殊字符"^"用来匹配以指定字符串开头的字符串。例如:
"^hello" :这个模式与字符串"hello,PHP world!"匹配,但是与"Say hello to you"不匹配。
特殊字符"$"用来匹配以指定字符串结尾的字符串。例如:
"you$" :这个模式与"How are you"匹配,与"your"不匹配。
当特殊字符"^"和"$"同时使用时,表示精确匹配。例如:
"^hello$" :这个模式只匹配字符串"hello"。
如果一个模式不包括"^"和"$",那么它与任何包含该模式的字符串匹配。例如: "you" :与字符串"What is your name?"是匹配的。
在该模式中的字母只是普通的字符,数字也是一样的。
如果要用到其他一些稍微复杂的字符,如标点符号和空白字符(比如空格、制表符等),就要要用到转义序列。所有的转义序列都用反斜杠("\")打头,例如制表符的转义序列是:"\t"。所以如果我们要检测一个字符串是否以制表符开头,可以用这个模式:
"^\t"
类似的,用"\n"表示换行,"\r"表示回车,反斜杠本身用"\\"表示,句号"."用"\."表示,依此类推。
如何使用字符簇?
如果要判断用户输入的电话号码、地址、EMAIL地址、信用卡号码等是否有效,用普通的基于字面的字符串比较是不够的。所以要用一种更好的方法来描述我们想要的模式,这就是字符簇。
比如,要建立一个表示所有元音字符的字符簇,就可以这样做:
"[AaEeIiOoUu]" :这个模式与任何元音字符匹配,但只能表示一个字符。
用特殊符号"-"可以表示一个字符的范围,如:
"[a-z]" | //匹配字母a-z,即所有的小写字母 |
"[A-Z]" | //匹配字母A-Z,即所有的大写字母 |
"[a-zA-Z]" | //匹配所有的字母 |
"[0-9]" | //匹配所有的数字 |
"[0-9\.\-]" | //匹配所有的数字,以及句号和减号 |
"[ \f\r\t\n]" | //匹配所有的白字符 |
同样的,这些也只匹配一个字符。
如果要匹配由一个小写字母和一位数字组成的字符串,比如"a4"、"b5"或"f1",但不是"aa4"、"b5a4" 或"f12"的话,用这个模式:
"^[a-z][0-9]$"
尽管[a-z]代表26个字母的范围,但在这里它只能与第一个字符是小写字母的字符串匹配。
我们已经知道"^"表示字符串的开头,但是当在一组方括号里使用"^"时,它表示"非"或"排除"的意思,常常用来剔除某个字符。还用前面的例子,我们要求第一个字符不能是数字: "^[^0-9][0-9]$"
这个模式与"a4"、"b5"及" 2"是匹配的,但与"12"、"66"是不匹配的。下面是几个排除特定字符的例子:
"[^a-z]" //除了小写字母以外的所有字符
"[^\\\/\^]" //除了(\)(/)(^)之外的所有字符
"[^\"\']" //除了双引号(")和单引号(')之外的所有字符
特殊字符"." (点,英文句号)在正规表达式中用来匹配除了"换行"之外的所有字符。所以模式"^.5$"与任何两个字符的、以数字5结尾和以其他非"换行"字符开头的字符串匹配。模式"."可以匹配任何字符串,除了空串和只包括一个"换行"的字符串。
PHP的正规表达式有一些内置的通用字符簇,列表如下:
字符簇
|
含义
|
"[[:alpha:]]" | 任何字母 |
"[[:digit:]]" | 任何数字 |
"[[:alnum:]]" | 任何字母和数字 |
"[[:space:]]" | 任何白字符 |
"[[:upper:]]" | 任何大写字母 |
"[[:lower:]]" | 任何小写字母 |
"[[:punct:]]" | 任何标点符号 |
"[[:xdigit:]]" | 任何16进制的数字,相当于[0-9a-fA-F] |
在很多的情况下,我们可能要匹配一个单词或一组数字。一个单词有若干个字母组成,一组数字有若干个单一的数字组成。我们用跟在字符或字符簇后面的花括?quot;{}"来确定前面的内容的重复出现的次数:假设x是一个数字,那么{x}表示"前面的字符或字符簇只出现x次";一个数字加逗号,{x,}的意思是"前面的内容出现x或更多的次数";两个用逗号分隔的数字,{x,y}表示"前面的内容至少出现x次,但不超过y次"。
字符簇
|
含义
|
"^[a-zA-Z_]$" | 所有的字母和下划线 |
"^[[:alpha:]]{3}$" | 所有的3个字母的单词 |
"^a$" | 字母a |
"^a{4}$" | 不是以字母a开头并且有4个字母的单词,比如Aaaa |
^a{2,4}$" | aa,aaa或aaaa |
"^a{1,3}$" | a,aa或aaa |
"^a{2,}$" | 包含多于两个a的字符串,比如aaa,aaaa,aaaaa |
"^a{2,}" | 以两个a开头的单词,如:aardvark和aaab,但apple不行 |
"a{2,}" | 包含有两个a的单词,如:baad和aaa,但Nantucket不行 |
"\t{2}" | 两个制表符 |
".{2}" | 所有的两个字符 |
我们可以把模式扩展到更多的单词或数字:
"^[a-zA-Z0-9_]{1,}$" | 所有包含一个以上的字母、数字或下划线的字符串 |
"^[0-9]{1,}$" | 所有的正数 |
"^\-{0,1}[0-9]{1,}$" | 所有的整数 |
"^\-{0,1}[0-9]{0,}\.{0,1}[0-9]{0,}$" | 所有的整数 |
最后一个例子我们可以这样考虑:所有以一个可选的负号(\-{0,1})开头(^)、跟着0个或更多的数字([0-9]{0,})、和一个可选的小数点(\.{0,1})再跟上0个或多个数字([0-9]{0,}),并且没有其他任何东西($)。
特殊字符"?"与"{0,1}"是相等的,它们都代表着:"0个或1个前面的内容"或"前面的内容是可选的"。所以:
"^\-{0,1}[0-9]{0,}\.{0,1}[0-9]{0,}$"
可以简化为:
^\-?[0-9]{0,}\.?[0-9]{0,}$
特殊字符"*"与"{0,}"是相等的,它们都代表着"0个或多个前面的内容"。字符" "与{1,}是相等的,表示"1个或多个前面的内容",所以上面的4个例子可以写成:
"^[a-zA-Z0-9_] $" | 所有包含一个以上的字母、数字或下划线的字符串 |
"^[0-9] $" | 所有的正数 |
"^\-?[0-9] $" | 所有的整数 |
"^\-?[0-9]*\.?[0-9]*$" | 所有的小数 |

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Even answering calls in Do Not Disturb mode can be a very annoying experience. As the name suggests, Do Not Disturb mode turns off all incoming call notifications and alerts from emails, messages, etc. You can follow these solution sets to fix it. Fix 1 – Enable Focus Mode Enable focus mode on your phone. Step 1 – Swipe down from the top to access Control Center. Step 2 – Next, enable “Focus Mode” on your phone. Focus Mode enables Do Not Disturb mode on your phone. It won't cause any incoming call alerts to appear on your phone. Fix 2 – Change Focus Mode Settings If there are some issues in the focus mode settings, you should fix them. Step 1 – Open your iPhone settings window. Step 2 – Next, turn on the Focus mode settings

Detailed explanation of the method of converting int type to string in PHP In PHP development, we often encounter the need to convert int type to string type. This conversion can be achieved in a variety of ways. This article will introduce several common methods in detail, with specific code examples to help readers better understand. 1. Use PHP’s built-in function strval(). PHP provides a built-in function strval() that can convert variables of different types into string types. When we need to convert int type to string type,

1. First open pycharm and enter the pycharm homepage. 2. Then create a new python script, right-click - click new - click pythonfile. 3. Enter a string, code: s="-". 4. Then you need to repeat the symbols in the string 20 times, code: s1=s*20. 5. Enter the print output code, code: print(s1). 6. Finally run the script and you will see our return value at the bottom: - repeated 20 times.

How to check if a string starts with a specific character in Golang? When programming in Golang, you often encounter situations where you need to check whether a string begins with a specific character. To meet this requirement, we can use the functions provided by the strings package in Golang to achieve this. Next, we will introduce in detail how to use Golang to check whether a string starts with a specific character, with specific code examples. In Golang, we can use HasPrefix from the strings package

Go language is a powerful and flexible programming language that provides rich string processing functions, including string interception. In the Go language, we can use slices to intercept strings. Next, we will introduce in detail how to intercept strings in Go language, with specific code examples. 1. Use slicing to intercept a string. In the Go language, you can use slicing expressions to intercept a part of a string. The syntax of slice expression is as follows: slice:=str[start:end]where, s

PHP String Operation: A Practical Method to Effectively Remove Spaces In PHP development, you often encounter situations where you need to remove spaces from a string. Removing spaces can make the string cleaner and facilitate subsequent data processing and display. This article will introduce several effective and practical methods for removing spaces, and attach specific code examples. Method 1: Use the PHP built-in function trim(). The PHP built-in function trim() can remove spaces at both ends of the string (including spaces, tabs, newlines, etc.). It is very convenient and easy to use.

PHP String Operation: Remove Extra Commas and Keep Only Commas Implementation Tips In PHP development, string processing is a very common requirement. Sometimes we need to process the string to remove extra commas and retain the only commas. In this article, I'll introduce an implementation technique and provide concrete code examples. First, let's look at a common requirement: Suppose we have a string containing multiple commas, and we need to remove the extra commas and keep only the unique comma. For example, replace "apple,ba

Converting a string to a floating point number is a common operation in PHP and can be accomplished through built-in methods. First make sure that the string is in a legal floating point format before it can be successfully converted to a floating point number. The following will detail how to convert a string to a floating point number in PHP and provide specific code examples. 1. Use (float) cast In PHP, the simplest way to convert a string into a floating point number is to use cast. The way to force conversion is to add (float) before the string, and PHP will automatically convert it
