2011-03-28 72 views
5
之外的所有字

在PHP中,如何使用preg_replace編寫正則表達式,以便刪除前面有空格的括號&中除「The」之外的任何短語。例如,正則表達式:匹配除

Concordia University (Anything But The) => Concordia University 
American University (The) => American University (The) 

回答

6

preg_replace是否支持負向預測?如果是這樣,你可以做這樣的事情...

\((?!The\)).*?\)

+1

是的,工作!謝謝!我還在排除的單詞中添加了「UCSD」:'preg_replace('/ \((!!(The | UCSD)\))。*?\)/','',$ subject) 'preg_replace'](http://php.net/manual/en/function.preg-replace.php)以獲取更多信息。 – ma11hew28 2011-03-31 16:10:29