標籤:pattern XML htm 限制 第一個 offset 技術分享 dom 應用
前面的話
Regex不能獨立使用,它只是一種用來定義字串的規則模式,必須在相應的Regex函數中應用,才能實現對字串的匹配、尋找、替換及分割等操作。前面介紹了Regex的基礎文法,本文將詳細介紹Regex函數
匹配與尋找
【preg_match()】
preg_match()函數用來執行一個Regex匹配,搜尋subject與pattern給定的Regex的一個匹配。返回pattern的匹配次數。它的值將是0次(不匹配)或1次,因為preg_match()在第一次匹配後將會停止搜尋。preg_match_all()不同於此,它會一直搜尋subject直到到達結尾。如果發生錯誤preg_match()返回FALSE
int preg_match ( string $pattern , string $subject [, array &$matches [, int $flags = 0 [, int $offset = 0 ]]] )
pattern表示要搜尋的模式,字串類型
subject表示輸入字串
如果提供了參數matches,它將被填充為搜尋結果。matches[0]將包含完整模式比對到的文本,matches[0]將包含完整模式比對到的文本,matches[1] 將包含第一個捕獲子組匹配到的文本,以此類推
flags可以被設定為以下標記:1、PREG_OFFSET_CAPTURE。如果傳遞了這個標記,對於每一個出現的匹配返回時會附加字串位移量(相對於目標字串的)。注意:這會改變填充到matches參數的數組,使其每個元素成為一個由第0個元素是匹配到的字串,第1個元素是該匹配字串在目標字串subject中的位移量;2、offset。通常,搜尋從目標字串的開始位置開始。選擇性參數offset用於指定從目標字串的某個未知開始搜尋(單位是位元組)
<?php//從URL中擷取主機名稱preg_match(‘@^(?:http://)?([^/]+)@i‘, "http://www.php.net/index.html", $matches);$host = $matches[1];//擷取主機名稱的後面兩部分preg_match(‘/[^.]+\.[^.]+$/‘, $host, $matches);//domain name is: php.netecho "domain name is: {$matches[0]}\n";?>
<?php $pattern = ‘/www\.[^\.\/]+\.com/i‘;$subject = ‘www.baidu.com,www.qq.com,www.cnblogs.com‘;preg_match($pattern,$subject,$matches);/*array (size=1) 0 => string ‘www.baidu.com‘ (length=13) */var_dump($matches);?>
【preg_match_all()】
preg_match_all()與preg_match()類似,不同的是preg_match()在第一次匹配之後就會停止搜尋,而函數preg_match_all()則會一直搜尋到指定字串的結尾,可以擷取到所有匹配到的結果
int preg_match_all ( string $pattern , string $subject [, array &$matches [, int $flags = PREG_PATTERN_ORDER [, int $offset = 0 ]]] )
<?php $pattern = ‘/www\.[^\.\/]+\.com/i‘;$subject = ‘www.baidu.com,www.qq.com,www.cnblogs.com‘;preg_match_all($pattern,$subject,$matches);/*array (size=1) 0 => array (size=3) 0 => string ‘www.baidu.com‘ (length=13) 1 => string ‘www.qq.com‘ (length=10) 2 => string ‘www.cnblogs.com‘ (length=15) */var_dump($matches);?>
【preg_grep()】
preg_grep()返回給定數組input中與模式pattern 匹配的元素組成的數組
array preg_grep ( string $pattern , array $input [, int $flags = 0 ] )
如果flags設定為PREG_GREP_INVERT,這個函數返回輸入數組中與 給定模式pattern不匹配的元素組成的數組
<?php $pattern = ‘/www\.[^\.\/]+\.com/i‘;$subject = [‘baidu.com‘,‘www.qq.com‘,‘www.cnblogs.com‘];var_dump (preg_grep($pattern,$subject));/*array (size=2) 1 => string ‘www.qq.com‘ (length=10) 2 => string ‘www.cnblogs.com‘ (length=15) */?>
替換
【preg_replace()】
preg_replace()執行一個Regex的搜尋替換,搜尋subject匹配pattern的部分,以replacement進行替換
mixed preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] )
replacement表示用於替換的字串或字串數組。如果這個參數是一個字串,並且pattern是一個數組,那麼所有的模式都使用這個字串進行替換。如果pattern和replacement都是數組,每個pattern使用replacement中對應的元素進行替換。如果replacement中的元素比pattern中的少,多出來的pattern使用Null 字元串進行替換
<?php$string = ‘April 15, 2016‘;$pattern = ‘/(\w+) (\d+), (\d+)/i‘;$replacement = ‘${1}1,$3‘;//April1,2016echo preg_replace($pattern, $replacement, $string);?>
<?php$string = ‘The quick brown fox jumped over the lazy dog.‘;$patterns = array();$patterns[0] = ‘/quick/‘;$patterns[1] = ‘/brown/‘;$patterns[2] = ‘/fox/‘;$replacements = array();$replacements[2] = ‘bear‘;$replacements[1] = ‘black‘;$replacements[0] = ‘slow‘;//The bear black slow jumped over the lazy dog.echo preg_replace($patterns, $replacements, $string);?>
【preg_replace_callback()】
preg_replace_callback()執行一個Regex搜尋並且使用一個回調進行替換
mixed preg_replace_callback ( mixed $pattern , callable $callback , mixed $subject [, int $limit = -1 [, int &$count ]] )
<?php// 將文本中的年份增加一年.$text = "April fools day is 04/01/2002\n";$text.= "Last christmas was 12/24/2001\n";// 回呼函數function next_year($matches){ // 通常: $matches[0]是完成的匹配 // $matches[1]是第一個捕獲子組的匹配 // 以此類推 return $matches[1].($matches[2]+1);}/*April fools day is 04/01/2003Last christmas was 12/24/2002 */echo preg_replace_callback( "|(\d{2}/\d{2}/)(\d{4})|", "next_year", $text);?>
【preg_filter()】
preg_filter() 執行一個Regex搜尋和替換,等價於preg_replace()除了它僅僅返回(可能經過轉化)與目標匹配的結果
mixed preg_filter ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] )
<?php$subject = array(‘1‘, ‘a‘, ‘2‘, ‘b‘, ‘3‘, ‘A‘, ‘B‘, ‘4‘); $pattern = array(‘/\d/‘, ‘/[a-z]/‘, ‘/[1a]/‘); $replace = array(‘A:$0‘, ‘B:$0‘, ‘C:$0‘); /*Array( [0] => A:C:1 [1] => B:C:a [2] => A:2 [3] => B:b [4] => A:3 [7] => A:4) */print_r(preg_filter($pattern, $replace, $subject)); /*Array( [0] => A:C:1 [1] => B:C:a [2] => A:2 [3] => B:b [4] => A:3 [5] => A [6] => B [7] => A:4) */print_r(preg_replace($pattern, $replace, $subject)); ?>
分割
【preg_split()】
preg_split()通過一個Regex分隔字串
array preg_split ( string $pattern , string $subject [, int $limit = -1 [, int $flags = 0 ]] )
如果指定limit,將限制分隔得到的子串最多隻有limit個,返回的最後一個子串將包含所有剩餘部分。limit值為-1,0或null時都代表"不限制";可以使用null跳過對flags的設定
flags可以是任何下面標記的組合(以位或運算 | 組合):PREG_SPLIT_NO_EMPTY——如果這個標記被設定,preg_split()將進返回分隔後的非空部分;PREG_SPLIT_DELIM_CAPTURE——如果這個標記設定了,用於分隔的模式中的括號運算式將被捕獲並返回;PREG_SPLIT_OFFSET_CAPTURE——如果這個標記被設定,對於每一個出現的匹配返回時將會附加字串位移量。注意:這將會改變返回數組中的每一個元素,使其每個元素成為一個由第0個元素為分隔後的子串,第1個元素為該子串在subject中的位移量組成的數組
<?php//使用逗號或空格(包含" ", \r, \t, \n, \f)分隔短語$keywords = preg_split("/[\s,]+/", "hypertext language, programming");/*Array( [0] => hypertext [1] => language [2] => programming) */print_r($keywords);?>
轉義
【preg_quote()】
preg_quote()轉義Regex字元
string preg_quote ( string $str [, string $delimiter = NULL ] )
Regex特殊字元有: . \ + * ? [ ^ ] $ ( ) { } = ! < > | : -
<?php$keywords = ‘$40 for a g3/400‘;$keywords = preg_quote($keywords, ‘/‘);echo $keywords; // 返回 \$40 for a g3\/400?>
前端學PHP之Regex函數