正則表達(dá)式基本函數(shù) 修飾符 元字符和需轉(zhuǎn)義字
Jun 08, 2016 pm 05:26 PM在php教程正則表達(dá)式中需要轉(zhuǎn)義的字符如下:
$^*()+={}[]|/:.?'"
注意:perl風(fēng)格中表達(dá)式要求以/開始和結(jié)尾,如:/food/ 表匹配字符 food
perl修飾符如下:
i 完成不區(qū)分大小寫的搜索
g 查找所有出現(xiàn)(all occurrences,完成全局搜索)
m 將一個字符串視為多行(m就表示多(multiple))。默認(rèn)情況下,^和$字符匹配字符串中的最開始和最末尾。使用m修飾符將使^和$匹配字符串中每行的開始
s 將一個字符串視為一行,忽略其中的所有換行符;它與m修飾符正好相反
x 忽略正則表達(dá)式中的空白和注釋
u 第一次匹配后停止。默認(rèn)情況下,將找到最后一個匹配字符結(jié)果。利用這個修飾符可以在第一次匹配后停止。進(jìn)而形式循環(huán)匹配。
元字符說明:
利用perl正則表達(dá)式還可以做另一件有用的事情,這就是使用各種元字符來搜索匹配。元字符(metacharacter)就是一個前面有反斜線的字母字符,表示某種特殊含義。以下是一些有用的元字符。
a 只匹配字符串開頭
b 匹配單詞邊界
b 匹配單詞邊界之外的任意字符
d 匹配數(shù)字字符,它與[0-9]相同
d 匹配非數(shù)字字符
s 匹配空白字符
s 匹配非空白字符
[] 包圍一個字符類,字符類包括:[0-9] [a-z] [a-za-z] 等類似。
() 包圍一個字符分組或定義一個反引用
$ 匹配行尾
^ 匹配行首
* 匹配前面的子表達(dá)式零次或多次。要匹配 * 字符,請使用 *
+ 匹配前面的子表達(dá)式一次或多次。要匹配 + 字符,請使用 +
? 匹配前面的子表達(dá)式零次或一次,或指明一個非貪婪限定符 ?
. 匹配除換行之外的任何字符
引出下一個元字符
w 匹配任何只包含下劃線和字母數(shù)字字符的字符串,它與 [a-za-z0-9_]相同
w 匹配沒有下劃線和字母數(shù)字字符的字符串
perl風(fēng)格函數(shù)有:
array preg_grep(string pattern, array input [, flags])
搜索數(shù)組中所有元素,返回由與某個模式匹配的所有元素組成的數(shù)組
php 4.3增加了一個可選參數(shù) flag,它接受一個值 preg_grep_invert 傳遞此標(biāo)志將得到與該模式不匹配的數(shù)據(jù)元素。
int preg_match(string pattern, string string [, array matches [, int flags [, int offset]]])
在字符串中搜索模式,如果存在返回true,否則返回false。
可選輸入?yún)?shù)matches可以包含搜索模式中包含的子模式的各個部分。默認(rèn)返回 匹配的字符串,當(dāng)有()子包圍時,會在數(shù)組后面輸出。
int preg_match_all(string pattern, string string, array pattern_array [, int order])
和函數(shù) preg_match一樣,不過 preg_match 只搜索一次,而 preg_match_all 會執(zhí)行循環(huán)搜索,返回所有匹配的結(jié)果。
mixed preg_replace(mixed pattern, mixed replacement, mixed str [, int limit])
用replacement替換pattern的所有出現(xiàn),并返回修改后的結(jié)果。
可選 limit 指定應(yīng)當(dāng)發(fā)生多少次匹配。不設(shè)置limit或設(shè)置為-1將替換所有出現(xiàn)的情況。

Hot AI Tools

Undress AI Tool
Undress images for free

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

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

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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

Convert basic data types to strings using Java's String.valueOf() function In Java development, when we need to convert basic data types to strings, a common method is to use the valueOf() function of the String class. This function can accept parameters of basic data types and return the corresponding string representation. In this article, we will explore how to use the String.valueOf() function for basic data type conversions and provide some code examples to

Method of converting char array to string: It can be achieved by assignment. Use {char a[]=" abc d\0efg ";string s=a;} syntax to let the char array directly assign a value to string, and execute the code to complete the conversion.

Replace characters (strings) in a string using Java's String.replace() function In Java, strings are immutable objects, which means that once a string object is created, its value cannot be modified. However, you may encounter situations where you need to replace certain characters or strings in a string. At this time, we can use the replace() method in Java's String class to implement string replacement. The replace() method of String class has two types:

Use Java's String.length() function to get the length of a string. In Java programming, string is a very common data type. We often need to get the length of a string, that is, the number of characters in the string. In Java, we can use the length() function of the String class to get the length of a string. Here is a simple example code: publicclassStringLengthExample{publ

Title: Example of using the Array.Sort function to sort an array in C# Text: In C#, array is a commonly used data structure, and it is often necessary to sort the array. C# provides the Array class, which has the Sort method to conveniently sort arrays. This article will demonstrate how to use the Array.Sort function in C# to sort an array and provide specific code examples. First, we need to understand the basic usage of the Array.Sort function. Array.So

When programming in PHP, we often need to merge arrays. PHP provides the array_merge() function to complete array merging, but when the same key exists in the array, this function will overwrite the original value. In order to solve this problem, PHP also provides an array_merge_recursive() function in the language, which can merge arrays and retain the values ??of the same keys, making the program design more flexible. array_merge

Hello everyone, today I will share with you the basic knowledge of Java: String. Needless to say the importance of the String class, it can be said to be the most used class in our back-end development, so it is necessary to talk about it.

Use Java's String.startsWith() function to determine whether a string starts with a specified prefix. In Java programming, we often need to process and determine strings. One common operation is to determine whether a string begins with a specified prefix. Java provides the startsWith() function of the String class, which can easily implement this function. String's startsWith() function The String class is the most commonly used class in Java
