PHP中應用Regex

來源:互聯網
上載者:User
PHP中運用Regex

1、函數

1)preg_match ―進行Regex匹配,相容perl的正則文法

int preg_match (string$pattern ,string $subject [,array$matches [,int $flags ]] )

加上標誌 PREG_OFFSET_CAPTURE 後,變成反向匹配

2)int ereg (string$pattern ,string $string [,array&$regs ])

註:preg_match函數比 ereg() 快。

3)preg_grep ― 返回與模式比對的數組單元

array preg_grep ( string $pattern , array $input [, int $flags ])
preg_grep() 返回一個數組,其中包括了 input 數組中與給定的 pattern 模式相匹配的單元。

同樣加入標誌 PREG_GREP_INVERT ,則變成反向匹配

(與正則文法中的?!對等)

如


$subject = "4|43|WINWORD.EXE|C:\DOCUME~1\iloveyou\LOCALS~1\Temp\~DF39E9.tmp||eew|1300763364|";
$pattern = '/\.(?!EXE|tmp).{3}/';
preg_match($pattern, $subject, $matches);
print_r($matches);
?>

2、如何讓Regex不區分大小寫

sql_regcase ― 產生用於不區分大小的匹配的Regex

$fl_array = preg_grep (sql_regcase("/^(ASSZs)/"), $array);

3、如何根據檔案尾碼過濾檔案名稱

$logs =preg_grep(sql_regcase('/^.*?\.(tmp|ini|pip).*?/'),$logstring,PREG_GREP_INVERT);

過濾掉尾碼名為tmp|ini|txt的檔案名稱,且不區分大小寫。

來自東子哥的Blog

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.