C# 正则表达式替换字符串中的第n个元素并添加自定义HTML标记
我有一个单词列表C# 正则表达式替换字符串中的第n个元素并添加自定义HTML标记,c#,regex,C#,Regex,我有一个单词列表“this”,“be-able”,“it”,我想在段落中找到这些单词,这样我就可以替换它们,保留它们的大小写 有本款: 这是我的文本,这就是为什么我要匹配它!因为这只是一个 文本,我想能够解决它。这是最后一句话 这一段 “this”被发现了5次,如果我决定替换第4个(“this”),我希望仍然能够保留T大写字母。现在您将看到,这实际上不是一个替换,而是一个添加问题,因为实际的替换将从这个到这个 因此,我的最后一段是: 这是我的文本,这就是为什么我要匹配它!由于这只是一个文本,我希
“this”,“be-able”,“it”
,我想在段落中找到这些单词,这样我就可以替换它们,保留它们的大小写
有本款:
这是我的文本,这就是为什么我要匹配它!因为这只是一个
文本,我想能够解决它。这是最后一句话
这一段
“this”
被发现了5次,如果我决定替换第4个(“this”
),我希望仍然能够保留T大写字母。现在您将看到,这实际上不是一个替换,而是一个添加问题,因为实际的替换将从这个到这个
因此,我的最后一段是:
这是我的文本,这就是为什么我要匹配它!由于这只是一个文本,我希望能够解决它这是本段的最后一句话
到目前为止,我的代码是:
List<string> words = new List<string>(new string[] { "this", "be able", "it"});
var paragraph = "This is my text and this is why I want to match it! As this is just a text, I would like to be able to solve it. This is the final phrase of this paragraph.";
//List<string>
for (int w = 0; w < words.Count; w++)
{
var foudItems = Regex.Matches(paragraph, @"\b" + words[w] + "\\b", RegexOptions.IgnoreCase);
if (foudItems.Count != 0)
{
Random rnd = new Random();
int rndWord = rnd.Next(0, foudItems.Count);
Regex.Replace(paragraph, @"\b" + words[w] + "\\b", "<strong>" + foudItems[rndWord] + "</strong>");
Console.WriteLine(paragraph);
}
//Regex.Replace()
Console.WriteLine(foudItems[0] + " " + foudItems[1]);
}
List words=新列表(新字符串[]{“this”、“be-able”、“it”});
var paragration=“这是我的文本,这就是我想要匹配它的原因!因为这只是一个文本,我希望能够解决它。这是本段的最后一个短语。”;
//名单
for(int w=0;w”+foudItems[rndWord]+“
”;
控制台。书面文件(第段);
}
//Regex.Replace()
Console.WriteLine(foudItems[0]+“”+foudItems[1]);
}
主要的问题是我不知道如何用正则表达式替换第n个单词。另一个问题是解决这个问题的方法很复杂,所以我愿意接受新的建议。如果要替换第n次出现的内容,可以使用MatchEvaluator
委托检查当前出现的索引,如果索引匹配不是要替换的,则返回未修改的匹配值。要跟踪当前索引,可以捕获局部变量:
int occurrenceToReplace = 4;
int index = 0;
MatchEvaluator evaluator = m => (++index == occurrenceToReplace)
? $"<strong>{m.Value}</strong>"
: m.Value;
text = Regex.Replace(text, @"\bthis\b", evaluator, RegexOptions.IgnoreCase);
样本输出:
这是我的文本,这就是我想要匹配的原因
它!由于这只是一个文本,我想
能够解决它。这是本书的最后一句话
段落
如果您想让regex端非常简单,可以使用以下算法:
List<string> words = new List<string>(new string[] { "this", "be able", "it" });
var paragraph = "This is my text and this is why I want to match it! As this is just a text, I would like to be able to solve it. This is the final phrase of this paragraph.";
//List<string>
foreach (string word in words)
{
var foundItems = Regex.Matches(paragraph, @"\b" + word + @"\b", RegexOptions.IgnoreCase);
if (foundItems.Count != 0)
{
var count = 0;
var toReplace = 3;
foreach (Match foudItem in foundItems)
{
count++;
if(count != toReplace)
continue;
var regex = $"(^.{{{foudItem.Index}}}){foudItem.Value}(.*)";
paragraph = Regex.Replace(paragraph, regex, $"$1<strong>{foudItem.Value}</strong>$2");
}
Console.WriteLine(paragraph);
}
Console.WriteLine(foundItems[0] + " " + foundItems[1]);
}
List words=新列表(新字符串[]{“this”、“be-able”、“it”});
var paragration=“这是我的文本,这就是我想要匹配它的原因!因为这只是一个文本,我希望能够解决它。这是本段的最后一个短语。”;
//名单
foreach(单词中的字符串)
{
var foundItems=Regex.Matches(段落@“\b”+word+@“\b”,RegexOptions.IgnoreCase);
如果(foundItems.Count!=0)
{
var计数=0;
var-toReplace=3;
foreach(匹配foundItems中的FoundItem)
{
计数++;
如果(计数!=重置)
继续;
var regex=$“(^.{{{foudItem.Index}}}}}{foudItem.Value}(.*)”;
段落=Regex.Replace(段落,Regex,$“$1{foudItem.Value}$2”);
}
控制台。书面文件(第段);
}
Console.WriteLine(foundItems[0]+“”+foundItems[1]);
}
不需要。只需使用backrefs(假设我将问题理解为“第四个匹配项应该看起来像输入”,而不是“我只想替换第四个匹配项”。@Tomalak从给定的预期示例中,我看到只有第四个匹配项应该用tagsHmm包装。。。我的假设是,这并不是比赛中荧光灯的典型表现。但是,是的,在这种情况下,没有办法绕过匹配计算器。@Tomalak嗯,正如Thomas所建议的,有一个选项可以从输入开始跳过符号,但我更愿意在这里使用计算器:)注意,我不清楚你的代码有什么用途。你能再解释一下“第四次发生”的事吗?是否只突出显示一个事件?如果是,其他搜索词有什么意义?代码中的随机数应该做什么?
string[] words = { "this", "be able", "it"};
var paragraph = @"This is my text and this is why I want to match it! As this is just
a text, I would like to be able to solve it. This is the final phrase of this paragraph.";
var random = new Random();
foreach(var word in words)
{
int count = Regex.Matches(paragraph, $@"\b{word}\b", RegexOptions.IgnoreCase).Count;
int occurrence = random.Next(count + 1);
paragraph = MakeStrong(paragraph, word, occurrence);
}
List<string> words = new List<string>(new string[] { "this", "be able", "it" });
var paragraph = "This is my text and this is why I want to match it! As this is just a text, I would like to be able to solve it. This is the final phrase of this paragraph.";
//List<string>
foreach (string word in words)
{
var foundItems = Regex.Matches(paragraph, @"\b" + word + @"\b", RegexOptions.IgnoreCase);
if (foundItems.Count != 0)
{
var count = 0;
var toReplace = 3;
foreach (Match foudItem in foundItems)
{
count++;
if(count != toReplace)
continue;
var regex = $"(^.{{{foudItem.Index}}}){foudItem.Value}(.*)";
paragraph = Regex.Replace(paragraph, regex, $"$1<strong>{foudItem.Value}</strong>$2");
}
Console.WriteLine(paragraph);
}
Console.WriteLine(foundItems[0] + " " + foundItems[1]);
}