c# - 正则表达式替换字符串中的第 n 个元素并添加自定义 HTML 标签

标签 c# regex

我有一个单词列表 “this”、“be able”、“it”,我想在段落中找到这些单词,这样我就可以替换并保留它们的大写。

有这一段:

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.

“this” 出现了 5 次,如果我决定替换第 4 个(“This”),我希望仍然能够保留 T 大写字母。现在您将看到这实际上不是替换,而是更多的添加问题,因为实际替换是从 thisThis

所以我的最后一段是:

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

主要问题是我不知道如何使用正则表达式只替换第 n 个单词。另一个问题是解决这个问题的复杂方法,所以我愿意接受新的建议。

最佳答案

如果你想替换第 n 次出现的东西,你可以使用 MatchEvaluator 委托(delegate)来检查当前出现的索引,如果索引匹配不是你想要替换的,则返回未修改的匹配值。要跟踪当前索引,您可以捕获局部变量:

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

现在回到您的问题 - 您可以编写将第 n 次出现的给定单词包装到 html 标记中的方法:

private static string MakeStrong(string text, string word, int occurrence)
{
    int index = 0;
    MatchEvaluator evaluator = m => (++index == occurrence)
         ? $"<strong>{m.Value}</strong>"
         : m.Value;
    return Regex.Replace(text, $@"\b{word}\b", evaluator, RegexOptions.IgnoreCase);
}

如果您想随机替换每个单词出现的一个,那么只需在循环中使用此方法即可:

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

示例输出:

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.

关于c# - 正则表达式替换字符串中的第 n 个元素并添加自定义 HTML 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43188835/

相关文章:

regex - 如何在Elasticsearch正则表达式中排除子字符串

regex - grep 中 '?' 的用法

c# - 在 Logging 中使用 StackFrame 和 MethodBase 的区别

用于匹配前向引用练习的正则表达式

c# 如何修剪字符串并检查它是否以数字结尾

c# - 获取DataContract中所有数据成员的名称

Python正则表达式匹配多行

javascript - 在 javascript 中执行没有 while 循环条件的组提取?

c# - 我想通过 SendInput API 发送一些 key

c# - 如何在 linq 连接 (lambda) 上添加 where 子句?