2013-02-06 133 views
0

我有以下文本行(META標題):正則表達式替換之間幷包括標籤

Buy [ProductName][Text] at a great price [/Text] from [ShopName] today. 

我這取決於什麼樣的價值觀我有更換。

我有工作,我需要,但是我無法找到正確的正則表達式來代替:

[Text] at a great price [/Text] 

的話(在方括號之間的ND)的變化,因此唯一將保持不變是:

[][/] 

即我可能還需要更換

[TestText]some test text[/TestText] with nothing. 

我有這樣的工作:

System.Text.RegularExpressions.Regex.Replace(SEOContent, @"\[Text].*?\[/Text]", @""); 

我推測的正則表達式:

[.*?].*?\[/.*?] 

會工作,但事實並非如此! - 我在ASP.NET C#編碼提前 感謝,

戴夫

+3

爲什麼只能轉義方括號之一? '\ [。*?\]。*?\ [/.*?\]' – paul

+0

完美,謝謝保羅,這很好,Regex不是我的強項! – dhardy

回答

1

使用名爲捕捉得到的節點名稱[..],然後找到它再次使用\ķ< ..> 。

(\[(?<Tag>[^\]]+)\][^\[]+\[/\k<Tag>\]) 

使用Ignore Pattern Whitespace和一個示例程序進行分解。

string pattern = @" 
(    # Begin our Match 
    \[    # Look for the [ escape anchor 
    (?<Tag>[^\]]+) # Place anything that is not antother ] into the named match Tag 
    \]    # Anchor of ] 
    [^\[]+   # Get all the text to the next anchor 
    \[/   # Anchor of the closing [...] tag 
    \k<Tag>  # Use the named capture subgroup Tag to balance it out 
    \]    # Properly closed end tag/node. 
)    # Match is done"; 

string text = "[TestText]some test text[/TestText] with nothing."; 

Console.WriteLine (Regex.Replace(text, pattern, "Jabberwocky", RegexOptions.IgnorePatternWhitespace)); 
// Outputs 
// Jabberwocky with nothing. 

順便說一句,我會實際創建標記化正則表達式(使用一個正則表達式如果與上面的圖案),並通過由名爲捕獲識別部分匹配內更換。然後在替換中使用匹配評估器替換已識別的令牌,如:

string pattern = @" 
(?(\[(?<Tag>[^\]]+)\][^\[]+\[/\k<Tag>\]) # If statement to check []..[/] situation 
    (          # Yes it is, match into named captures 
    \[ 
    (?<Token>[^\]]+)      # What is the text inside the [ ], into Token 
    \] 
    (?<TextOptional>[^\[]+)    # Optional text to reuse 
    \[ 
    (?<Closing>/[^\]]+)     # The closing tag info 
    \] 
) 
|          # Else, let is start a new check for either [] or plain text 
(?(\[)         # If a [ is found it is a token. 
    (         # Yes process token 
    \[ 
    (?<Token>[^\]]+)      # What is the text inside the [ ], into Token 
    \] 
    ) 
    |          # Or (No of the second if) it is just plain text 
    (?<Text>[^\[]+)      # Put it into the text match capture. 
) 
) 
"; 


string text = @"Buy [ProductName] [Text]at a great price[/Text] from [ShopName] today."; 

Console.WriteLine (
Regex.Replace(text, 
       pattern, 
       new MatchEvaluator((mtch) => 
       { 

       if (mtch.Groups["Text"].Success)   // If just text, return it. 
        return mtch.Groups["Text"].Value; 

       if (mtch.Groups["Closing"].Success)  // If a Closing match capture group reports success, then process 
       { 
        return string.Format("Reduced Beyond Comparison (Used to be {0})", mtch.Groups["TextOptional"].Value); 
       } 

        // Otherwise its just a plain old token, swap it out. 
        switch (mtch.Groups["Token"].Value) 
        { 
        case "ProductName" : return "Jabberwocky"; break; 
        case "ShopName" : return "StackOverFlowiZon"; break; 
        } 


        return "???"; // If we get to here...we have failed...need to determine why. 

       }), 
       RegexOptions.IgnorePatternWhitespace | RegexOptions.ExplicitCapture)); 
// Outputs: 
// Buy Jabberwocky Reduced Beyond Comparison (Used to be at a great price) from StackOverFlowiZon today. 
+0

偉大的東西,這第一次很好! – dhardy

+0

這似乎取代了方括號'[]中的ANY和ALL塊 – Sinaesthetic

相關問題