Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 正则表达式使用列表映射文本字符串以避免html标记重叠_C# - Fatal编程技术网

C# 正则表达式使用列表映射文本字符串以避免html标记重叠

C# 正则表达式使用列表映射文本字符串以避免html标记重叠,c#,C#,我希望能够映射此文本,以便添加HTML标记 我以后在任何地方都可以通过搜索不同的单词来决定 预定义的单词列表。我需要一个解决方案,允许存储 并处理这些数据。我想的是使用 regex.matches以获得匹配的单词索引位置,因此在末尾I 将整个文本映射到相应的单词索引。 现在文本将没有任何意义,因为我只是为了测试而写 所以我想再次使用这个词 让我们来看看这个单词列表:“我想要”、“愿意”、“单词”、“单词” 到目前为止,我的代码是: string text = "I would like

我希望能够映射此文本,以便添加HTML标记 我以后在任何地方都可以通过搜索不同的单词来决定 预定义的单词列表。我需要一个解决方案,允许存储 并处理这些数据。我想的是使用 regex.matches以获得匹配的单词索引位置,因此在末尾I 将整个文本映射到相应的单词索引。 现在文本将没有任何意义,因为我只是为了测试而写 所以我想再次使用这个词

让我们来看看这个单词列表:“我想要”、“愿意”、“单词”、“单词”

到目前为止,我的代码是:

    string text = "I would like to be able to map this text so I can add HTML tags anywhere I later decide by searching for different words from a predefined list of words. I need a solution that would allow to store thins kind of information. What I have in ming is to use regex.mactches to get the matched word index position so in the end I would have mapped the whole text with their corresponding word index. Now the text will not have any meaning as I'm only writing for testing purposes so I would like to use this word once again.";
    List<string> keywords = new List<string>(new string[] { "I would like", "would", "word", "words" });

    foreach (var k in keywords)
    {

        var foundItems = Regex.Matches(text, $@"\b{k}\b", RegexOptions.IgnoreCase);

    }
在添加第一个html标记(
希望
)后,我想将文本字符串的索引标记为“已占用”,以便从索引0到20,我无法再进行添加。如果我尝试将HTML标记添加到从索引位置2开始的第二个单词(“将”),我会看到它是“已占用”的,忽略该位置并移动到下一个位置,这样数组将只剩下3个可用位置(177到182、325到330、480到485)


请注意,我无法修改单词列表中元素的顺序。

。我敢打赌,如果你在谷歌上搜索一下,你会找到你喜欢的方法。这似乎是一件非常常见的事情。您提到的解决方案正是我试图避免的:重叠html标记。我对此进行了研究,但找不到任何可靠的信息,因此,如果您对此有任何想法,我将洗耳恭听:)
new I_Would_like = {0 to 12, 478 to 490}
new would = {2 to 5,177 to 182, 325 to 330, 480 to 485)
new word = {289 to 293, 383 to 491, 503 to 507}
new words = {115 to 120, 147 to 152}