2017-04-26 162 views
2

我有一個字符串包含具有相似圖案標記的內容象下面這樣:如何從C#中的字符串中提取多個子字符串匹配?

This is a <ss type="">(example)</ss> string which <ss type="">(contains)</ss> tagged contents. 

預期結果是:

This is a <ss type="example">(example)</ss> string which <ss type="contains">(contains)</ss> tagged contents. 

我試圖通過RegularExpression提取的標籤的內容列表,並已提取的標記文本列表,並將標記文本放在雙引號中作爲值type,並將新字符串替換爲舊字符串。

但問題是,因爲Regex.Replace();是繼同Regex模式,它取代所有標籤內容與標籤內容的最後一個元素列表如下圖所示:

This is a <ss type="contains">(contains)</ss> string which <ss type="contains">(contains)</ss> tagged contents. 

我工作的代碼如下:

StringBuilder resultText= new StringBuilder(@"This is a <ss type="">(example)</ss> string which <ss type="">(contains)</ss> tagged contents."); 
string overallPattern = @"<ss\stype=""([a-zA-Z]*)"">(.*?)</ss>"; 
List<string> matchList = new List<string>(); 
List<string> contentList = new List<string>(); 
StringBuilder sb; 
Regex overallRegex = new Regex(overallPattern, RegexOptions.None); 
string resultContent = resultText.ToString(); 
foreach (Match match in overallRegex.Matches(resultContent)) 
    { 
     string matchResult = match.ToString(); 
     matchList.Add(matchResult); 
     string content = matchResult.Split('(', ')')[1]; 
     contentList.Add(content); 
    } 
for (int j = 0; j < matchList.Count; j++) 
    { 
     sb = new StringBuilder(); 
     sb.Append(matchList[j].Insert(10, string.Format(contentList[j]))); 
     resultContent = Regex.Replace(resultContent, overallPattern, sb.ToString()); 
     resultText = new StringBuilder(); 
     resultText.Append(resultContent); 
    } 

我的問題是:

如何通過命令將正確的標記文本放入雙引號中?

+0

你檢查你的正則表達式正如我看到它的不匹配任何 –

+0

確定的原因是這是一個其必須是。我已經編輯你的問題 –

+0

@ S.Petrosov感謝你的努力。它匹配。其實這個字符串來自其他地方,像'\「'這樣的雙引號轉義字符,我認爲它和你的字符串是一樣的,上面的字符串只是用來演示的, –

回答

2

我們根據您的replace標籤內容來創建動態Regex。例如,<ss\stype="([a-zA-Z]*)">\(example\)<\/ss>將僅替換內容中具有example的一個標籤。請檢查這一點,它會根據你的描述工作。

代碼:以上代碼的

//StringBuilder resultText = new StringBuilder(@"This is a <ss type="""">(example)</ss> string which <ss type="""">(contains)</ss> tagged contents."); 
//You have to use """" instead on "" in this line 
StringBuilder resultText = new StringBuilder(@"This is a <ss type="""">(example)</ss> string which <ss type="""">(contains)</ss> tagged contents."); 
string overallPattern = @"<ss\stype=""([a-zA-Z]*)"">(.*?)</ss>"; 
List<string> matchList = new List<string>(); 
List<string> contentList = new List<string>(); 
StringBuilder sb; 
Regex overallRegex = new Regex(overallPattern, RegexOptions.None); 
string resultContent = resultText.ToString(); 
foreach (Match match in overallRegex.Matches(resultContent)) 
{ 
    string matchResult = match.ToString(); 
    matchList.Add(matchResult); 
    string content = matchResult.Split('(', ')')[1]; 
    contentList.Add(content); 
} 
for (int j = 0; j < matchList.Count; j++) 
{ 
    //Dynamic Regex based on tag content for replace 
    overallPattern = @"<ss\stype=""([a-zA-Z]*)"">\("+ contentList[j] + "\\)</ss>"; 
    sb = new StringBuilder(); 
    sb.Append(matchList[j].Insert(10, string.Format(contentList[j]))); 
    resultContent = Regex.Replace(resultContent, overallPattern, sb.ToString()); 
    resultText = new StringBuilder(); 
    resultText.Append(resultContent); 
} 

輸出:

This is a <ss type="example">(example)</ss> string which <ss type="contains">(contains)</ss> tagged contents.

+0

Yay!它在工作!我不熟悉'Regex',動態部分正是我困惑的地方,其實我差不多在那裏,謝謝! –

0

您只需要更換

resultContent = Regex.Replace(resultContent, overallPattern, sb.ToString()); 

通過這樣的:

resultContent = resultContent.Replace(matchList[j], sb.ToString()); 
+0

謝謝你的努力,我做了這個部分。正是我所要求的, –