首頁  >  文章  >  後端開發  >  PHP 正規表示式

PHP 正規表示式

PHPz
PHPz原創
2024-08-29 13:01:52400瀏覽

正規表示式可以定義為在單行中產生的模式匹配演算法。這些對於驗證檢查和模板識別是有影響的。元字元使用戶能夠處理複雜的模式。因此,PHP 對正規表示式的支援有助於提高 PHP 程式設計的程式碼品質。任何正規表示式都是通用模式或一組字元的序列,用於針對給定的主題字串提供模式匹配功能。它也稱為 RegExp 或 RegEx。它也被認為是基於模式表示法的小型程式語言,用於文字字串解析。

廣告 該類別中的熱門課程 PHP 開發人員 - 專業化 | 8 門課程系列 | 3次模擬測驗

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

PHP函數中的2組正規表示式

下面給出支持2組正規表示式:

  • POSIX 正規表示式
  • PERL 風格正規表示式

1. POSIX 正規表示式

這被定義為字元集,其中任何單一字元都需要與輸入字串相符。這些表達式在 [].

中定義

範例:

  • [0-9]: 這旨在過濾從 0 到 9 的任何十進位字串。
  • [a-Z]: 這旨在過濾從小寫「a」到大寫「Z」的任何字元。

為了使過濾器更加具體,開發了一種標準語法,包括正規表示式和特殊字符,稱為量詞。它還提供有關頻率的信息,即括號字元或一組字元的出現或實例數量以及數量。

找出不同量詞描述的表格:

Quantifier Description
S+ Filters out a string having at least one ‘s’.
S* Filters out a string having zero or more ‘s’.
S? Filters out a string having zero or one ‘s’.
S{N} Filters out a string having a sequence of N ‘s’.
S$ Filters out a string having ‘s’ at the end.
^S Filters out a string having ‘s’ at the beginning.
量詞

描述

S+ 過濾掉至少有一個「s」的字串。 S* 過濾掉具有零個或多個「s」的字串。 S? 過濾掉包含零個或一個「s」的字串。 S{N} 過濾出具有 N 個「s」序列的字串。 S$ 過濾掉末尾帶有「s」的字串。 ^S 過濾掉以「s」開頭的字串。 表> PHP 也支援預定義字元範圍/類別的匹配功能。
predefined character class Description
[[:space:]] Filters out a string having a space.
[[:alpha:]] Filters out a string having alphabetic characters a-A through z-Z.
[[:digit:]] Filters out a string having numerical 0 to 9.
[[:alnum:]] Filters out a string having alphanumeric characters a-A through z-Z and numerical 0 to 9.
範例: 預定義字元類別 描述 [[:空格:]] 過濾掉含有空格的字串。 [[:alpha:]] 過濾掉包含字母字元 a-A 到 z-Z 的字串。 [[:數字:]] 過濾掉數字0到9的字串。 [[:alnum:]] 過濾掉包含字母數字字元 a-A 到 z-Z 以及數字 0 到 9 的字串。 表>

對於 POSIX 正規表示式,PHP 整合了各種函數,可以使用 POSIX 風格的正規表示式執行各種操作。

功能說明如下表:

POSIX Regex function Description
ereg() Used to search a string specified by or by pattern and to return true if the matching is found.
ereg_replace() Used to search a string specified by or by pattern and replace with replacement if the matching is found.
eregi() Used to perform non-case sensitive search for a string specified by or by pattern and to return true if the matching is found.
eregi_replace() Used to perform non-case sensitive for a string specified by or by pattern and replace with replacement if the matching is found.
split() Used to divide the string into separate elements based on boundaries that matches the searching pattern.
spliti() Used to perform non-case sensitive for the string to divide it into separate elements based on boundaries that matches the searching pattern.
sql_regcase() A utility function that convert each character from the input value into a bracketed expression making two characters.

2. PERL 風格正規表示式

這種類型的正規表示式模式類似於 POSIX 正規表示式,但使用元字元和識別碼建立。此正規表示式的語法可與 POSIX 樣式互換。

a。元字元:前面帶有表示特定意義的反斜線的字母字元稱為元字元。

PHP 腳本支援多種元字符,用作 Perl 類型正規表示式,如下所述:

Meta character Description
. Single character
d A digit character
D Non-digit character
s white space character e.g. SPACE, NEW LINE, TAB
S Non- white space character
w A word character
W Non-word character
[aeiou] Filters the matched character out of the given set
[^aeiou] Filters the unmatched character out of the given set
(set1|set2|set3) Filters the matched element that matches to any of the given alternatives
元字元

描述 。 單一字元 d 數字字元 D 非數字字元 s 空白字符,例如空白鍵、換行符、製表符 S 非空白字元 w 一個單字字元 西 非單字字元 [aeiou] 從給定集合中過濾出匹配的字元 [^aeiou] 從給定集合中過濾掉不匹配的字元 (設定1|設定2|設定3) 過濾與任何給定替代項匹配的匹配元素 表>

b。修飾符:

這些元素使用戶能夠利用正規表示式來獲得額外的靈活性。
Modifier Description
g Finds matchings globally.
cg Enable continue global search even after matching fails.
i Instructs to perform case insensitive search.
s Use the character ‘.’ to search for new line character.
m In case of input string containing new line or carriage return character, ‘^’ and ‘$’ are used to match for new line boundary.
x Permits to use white space to improve the clarity of the expression.
o Restrict the evaluation of the expression to occur only once.
下表列出了各種修飾符及其功能: 修飾符 描述 克 在全球範圍內尋找匹配項。 cg 即使匹配失敗也能繼續進行全域搜尋。 我 指示執行不區分大小寫的搜尋。 s 使用字元“.”搜尋換行符。 米 如果輸入字串包含換行符或回車符,則使用「^」和「$」來匹配換行邊界。 x 允許使用空白來提高表達式的清晰度。 o 限製表達式的計算僅發生一次。 表>

與 POSIX 正規表示式函數類似,PHP 也提供了一些與 PERL 風格正規表示式相容的特定函數。

一些主要功能討論如下:

PERL style regexpcompitable function Description
preg_match() Return the first occurrence of the matching pattern.
preg_match_all() Return all occurrences of the matching pattern.
preg_split() Splits the string input into several elements based on the given regexp pattern as input.
Preg_quote() Used to quote the characters of the regex.
preg_grep() Used to find all the matching elements from array input.
preg_replace() Used to find matching element and replace it with the given replacement.

PHP 正規表示式範例

下面的範例示範了應用程式

程式碼片段旨在掃描輸入字串,並透過將給定的正規表示式定義為邊界,將給定的輸入拆分為多個元素。

代碼:

<?php
// Declaring a regex
$regex = "([0-9]+)";
// Defining the input string
$inputstr = "String_a 1 String_b 2 String_c 3";
//Splitting the input string based on matching regex expression
$result = preg_split ($regex, $inputstr);
// Displaying result
echo $result[0];
echo "\n";
echo $result[1];
echo "\n";
echo $result[2];
echo "\n";
?>

輸出

函數 preg_split() 將輸入字串分成 3 部分,元素「1」、「2」和「3」被標記為邊界。

PHP 正規表示式

以上是PHP 正規表示式的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
上一篇:PHP 魔法常數下一篇:PHP 魔法常數