如何从C#中的字符串中提取多个子字符串匹配?

时间:2017-04-26 04:00:45

标签: c# asp.net regex

我有一个字符串包含带有类似模式的标记内容,如下所示:

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模式,它会将所有标记内容替换为标记内容列表的最后一个元素,如下所示:< / p>

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);
     }

我的问题是:

  

如何按顺序将正确的标记文本放入双引号中?

2 个答案:

答案 0 :(得分:2)

您必须根据Regex的代码内容创建动态replace。例如,<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.

答案 1 :(得分:0)

您只需要替换

class Dogs(Mammals):
    def __init__(self):
        #add new attribute
        self.someattribute = 'value'
        super(Mammals, self).__init__()

由此:

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