2012-06-27 69 views
1

的所有事件我有一個字符串,是這個形式:獲取子串匹配模式

"Some text [asd](fgh) Lorem Ipsum [qwert](y)" 

你可能會認識到這是降價的語法。

我想這樣的鏈接替換模式的所有出現:

<a href="fgh">asd</a> 

什麼是做到這一點的最好方法是什麼?我可以做沒有問題的實際替換,但我不確定如何識別子字符串。我懷疑正則表達式是要走的路嗎?

在此先感謝您的幫助!

+1

如果你認爲正則表達式是去那麼唯一的辦法,我認爲你應該提供實際的例子。正則表達式具有高度特定的範圍。 – npinti

+0

該死的,我搞砸了格式化,我已經AFK了幾個小時,所以我沒有抓到它。 –

回答

2

是的,我認爲正則表達式是OK的位置:

resultString = Regex.Replace(subjectString, 
    @"\[  # Match [ 
    (  # Match and capture in group 1: 
    [^][]* # Any number of characters except brackets 
    )  # End of capturing group 1 
    \]  # Match ] 
    \(  # Match (
    (  # Match and capture in group 2: 
    [^()]* # Any number of characters except parentheses 
    )  # End of capturing group 2 
    \)  # Match)", 
    "<a href=\"$2\">$1</a>", RegexOptions.IgnorePatternWhitespace); 
+0

這就像一個魅力!非常感謝! –