gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 23:13:25 24 4
gpt4 key购买 nike

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

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

我的问题是:

How can I put the right tag text into its double quote by order?

最佳答案

您必须创建动态 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.

关于c# - 如何从 C# 中的字符串中提取多个子字符串匹配项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43624796/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com