2016-01-14 43 views
0

我有一個正則表達式:如何將PCRE正則表達式轉換爲PHP?

/constructor.*?\nbegin\n(^(?:(?!inherited).)*$\n)+?end/igm; 

(它搜索的Delphi構造不繼承關鍵字)。當我在這裏使用它:http://www.regextester.com/這是工作相當不錯

例如,您可以測試它串

constructor TAPISession.Create; 
begin 

    LastActivity:=Now; 
    CS:= TCriticalSection.Create; 
    APIObj:= TAdvancedServerObject.Create; 
    CachedDomainsList:=TCachedDomainsList.Create; 
    CachedAccountsList:= TCachedAccountsList.Create; 
end; 

但是,如果我在PHP中使用它(preg_match_all)它不工作

preg_match_all('/constructor.*?\nbegin\n(^(?:(?!inherited).)*$\n)+?end;/im',$string,$matches); 

任何幫助,將不勝感激

+2

你確定你在那裏只有'\ n'換行符?也許它是'\ r \ n'而不是? –

+0

男人,我很愚蠢:D非常感謝,第二小時救了我:D –

+0

你是在捕獲組1之後?通過'$ matches [1]'來訪問它。 –

回答

0

謝謝Marc B,所以答案很簡單,只是檢查還回車

preg_match_all('/constructor.*?\r?\nbegin\r?\n(^(?:(?!inherited).)*$\r?\n)+?end;/im',$string,$matches); 
相關問題