2014-10-09 20 views
-1

大多數情況下,當我查找正則表達式模式時,我從一些php代碼中獲取它,然後我需要「轉換」到Delphi。 Delphi中的一個大問題是,在Delphi中,php轉義不起作用,但如果你在大多數情況下忽略它們,一切都會好的。但是在下一個示例中,如果從模式中刪除/ x,函數preg_match_all不會輸出任何值。如何在Delphi中使用php模式正則表達式

這是在PHP代碼:

$pattern = "/\n". 
    "\s(counters?\\([^)]*\\))|\n". 
    "\A(counters?\\([^)]*\\))|\n". 
    "\s([\"']) ((?:[^\"']|\\\\[\"'])+)(?<!\\\\)\\3|\n". 
    "\A([\"']) ((?:[^\"']|\\\\[\"'])+)(?<!\\\\)\\5|\n" . 
    "\s([^\s\"']+)|\n" . 
    "\A([^\s\"']+)\n". 
    "/xi"; 

    preg_match_all($pattern, '"File " counter(File)', $matches, PREG_SET_ORDER); 

這是輸出:

array (size=2) 
    0 => 
    array (size=7) 
     0 => string '"File "' (length=7) 
     1 => string '' (length=0) 
     2 => string '' (length=0) 
     3 => string '' (length=0) 
     4 => string '' (length=0) 
     5 => string '"' (length=1) 
     6 => string 'File ' (length=5) 
1 => 
    array (size=2) 
     0 => string ' counter(File)' (length=14) 

這是我在Delphi中所做的:

type 
TMatches = Array of Array of String; 
var 
    matches: TMatches; 
pattern := '/\n'+ 
    '\s(counters?\\([^)]*\\))|\n'+ 
    '\A(counters?\\([^)]*\\))|\n'+ 
    '\s([\"'']) ((?:[^\"'']|\\\\[\"''])+)(?<!\\\\)\\3|\n'+ 
    '\A([\"'']) ((?:[^\"'']|\\\\[\"''])+)(?<!\\\\)\\5|\n'+ 
    '\s([^\s\"'']+)|\n'+ 
    '\A([^\s\"'']+)\n'+ 
    '/xi'; 

    RegExMatchAll(pattern,'"Page " counter(page)',matches); 

功能RegExMatchAll我從這裏得到它,並在大多數情況下工作:

procedure RegExMatchAll(Pattern: String; Subject: String; out Matches: TMatches); 
    var D, sD: integer; RegEx: TRegEx; RegGroupColl: TGroupCollection; RegColl: TMatchCollection; 
    begin 
     RegEx := TRegEx.Create(Pattern); 
     RegColl := RegEx.Matches(Subject); 
     SetLength(Matches, RegColl.Count); // Numero de coincidencias [array [X]] 
     for D := 0 to RegColl.Count - 1 do 
     begin 
     RegGroupColl := RegColl.Item[D].Groups; 
     SetLength(Matches[D], RegGroupColl.Count); // Numero de grupos [array [D][sD]] 
     for sD := 0 to RegGroupColl.Count - 1 do Matches[D][sD] := RegGroupColl.Item[sD].Value; 
     end; 
    end; 

此外,我\ n更改爲#13#10,並沒有什麼關係,匹配數組是空的。我知道如果一個困難的模式正則表達式,但如果最終我們可以解決這個問題,這將是美好的,有很多PHP的正則表達式代碼,如果我們能夠知道如何在Delphi中使用它,這對Delphi開發人員來說將會很好。

+0

你正在使用哪種正則表達式?自從使用PCRE的XE以來,Delphi已經構建了正則表達式支持。各種正則表達式都有文檔,所以不需要卡住。你可以閱讀文檔。這比剪切和粘貼你不理解的正則表達式要容易得多,然後使用試驗和錯誤。 – 2014-10-09 07:05:29

+0

儘管我不喜歡RTFMish的答案,但我必須在這裏同意David的觀點。一個好的首發將是[regular-expressions.info的Delphi頁面](http://www.regular-expressions.info/delphi.html),它講述如何用'.Item []'來獲得比賽等。我不熟悉與德爾福,但這應該有助於你走上正確的軌道。 PHP和Delphi都使用相同的PCRE正則表達式,所以正則表達式本身的語法不應該有很大的不同。 – funkwurm 2014-10-09 07:20:02

+0

Delphi代碼中似乎有太多反斜槓。請記住,其中大約一半是爲了逃避PHP字符串,但這在Delphi字符串中不是必需的。 – 2014-10-09 12:18:38

回答

2

據我所知,PHP的正則表達式支持是建立在PCRE上的。就像德爾福那樣,你看起來正在使用什麼。雖然我在那裏猜測,因爲你沒有說明這一點。不過,我認爲這是一個合理安全的假設。

PHP中的x修飾符對應於PCRE_EXTENDED標誌。在Delphi中相應的設置是roIgnorePatternSpace選項。當您致電Matches時,請通過該選項。

一邊。請勿使用非現場鏈接發佈重要的代碼。我們不應該離開這個問題去了解你的代碼是什麼。讀者應該能夠從問題中直接獲取所有信息。事實上,讀者會想知道這個答案是如何辨別你使用哪個Delphi正則表達式庫的,並且你打電話給Matches

+0

是的,問題是添加roIgnorePatternSpace選項。與此,完美的作品。 – user3779155 2014-10-09 10:22:35