正規表示式可以定義為在單行中產生的模式匹配演算法。這些對於驗證檢查和模板識別是有影響的。元字元使用戶能夠處理複雜的模式。因此,PHP 對正規表示式的支援有助於提高 PHP 程式設計的程式碼品質。任何正規表示式都是通用模式或一組字元的序列,用於針對給定的主題字串提供模式匹配功能。它也稱為 RegExp 或 RegEx。它也被認為是基於模式表示法的小型程式語言,用於文字字串解析。
廣告 該類別中的熱門課程 PHP 開發人員 - 專業化 | 8 門課程系列 | 3次模擬測驗開始您的免費軟體開發課程
網頁開發、程式語言、軟體測試及其他
下面給出支持2組正規表示式:
這被定義為字元集,其中任何單一字元都需要與輸入字串相符。這些表達式在 [].
中定義範例:
為了使過濾器更加具體,開發了一種標準語法,包括正規表示式和特殊字符,稱為量詞。它還提供有關頻率的信息,即括號字元或一組字元的出現或實例數量以及數量。
找出不同量詞描述的表格:
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. |
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. |
對於 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. |
這種類型的正規表示式模式類似於 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 |
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. |
與 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 // 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中文網其他相關文章!