首页 > 解决方案 > C# RegEx:如何仅匹配文本行中间单词内的字符串?

问题描述

使用 C# RegEx,我只需要在文本行“123xxx123 123 123xxx xxx123xxx xxx123 123xxx123”的中间词中匹配字符串“123”。

它应该只匹配内部的“123”,而不是第一个或最后一个单词:“123xxx123 [123] [123]xxx xxx[123]xxx xxx[123] 123xxx123”。

我尝试了消极的前瞻/后视无济于事。

基本上,我需要支持一个 Find 实用程序,该实用程序具有用于查找匹配(可能是多字)等于或在起始词、中间词、结尾词、行中任何位置的选项。

    string pattern_empty_line = @"(" + @"^$" + @")";
    string pattern_whole_line = @"(" + @"^" + text + @"$" + @")";

    string pattern_whole_word = @"(" + @"\b" + text + @"\b" + @")";
    string pattern_prefix = @"(" + @"\S+?" + text + @")";
    string pattern_suffix = @"(" + text + @"\S+?" + @")";
    string pattern_prefix_and_suffix = @"(" + @"\S+?" + text + @"\S+?" + @")";

    // Any Wordness
    string pattern_anywordness_start = @"(" + pattern_whole_line + "|"
                                            + @"(" + @"^" + pattern_whole_word + @")" + "|"
                                            + @"(" + @"^" + pattern_prefix + @")" + "|"
                                            + @"(" + @"^" + pattern_suffix + @")" + "|"
                                            + @"(" + @"^" + pattern_prefix_and_suffix + @")"
                                     + @")";
    string pattern_anywordness_end = @"(" + pattern_whole_line + "|"
                                          + @"(" + pattern_whole_word + @"$" + @")" + "|"
                                          + @"(" + pattern_prefix + @"$" + @")" + "|"
                                          + @"(" + pattern_suffix + @"$" + @")" + "|"
                                          + @"(" + pattern_prefix_and_suffix + @"$" + @")"
                                   + @")";
    string pattern_anywordness_not_middle = @"(" + pattern_whole_line + "|" + pattern_anywordness_start + "|" + pattern_anywordness_end + @")";
    string pattern_anywordness_middle = @"(" + @"\b" + @".*" + text + @".*" + @"\b" + @")";
    string pattern_anywordness_anywhere = @"(" + text + @")";

    // Part of word
    string pattern_partword_start = @"(" + pattern_prefix + "|" + @"^" + pattern_prefix_and_suffix + @")";
    string pattern_partword_middle = @"(" + @"(?<!^)" + pattern_prefix_and_suffix + @"(?!$)" + @")";
    string pattern_partword_end = @"(" + pattern_prefix_and_suffix + @"$" + pattern_suffix + "|" + @")";
    string pattern_partword_anywhere = @"(" + pattern_partword_start + "|" + pattern_partword_middle + "|" + pattern_partword_end + @")";

    // Whole word
    string pattern_wholeword_start = @"(" + pattern_whole_line + "|" + @"^" + text + @"\b" + @")";
    string pattern_wholeword_middle = @"(" + pattern_whole_line + "|" + @"(?<!^)" + @"\b" + text + @"\b" + @"(?!$)" + @")";
    string pattern_wholeword_end = @"(" + pattern_whole_line + "|" + @"\b" + text + @"$" + @")";
    string pattern_wholeword_anywhere = @"(" + pattern_wholeword_start + "|" + pattern_wholeword_middle + "|" + pattern_wholeword_end + @")";

我能够匹配除中间词之外的所有单词,甚至能够匹配“非中间词”(参见上面的代码)。最好在“NOT start words”和“NOT final words”中找到匹配项。

此外,所需的匹配本身可能是一个多词,因此请考虑到这一点。

标签: c#regextext-editor

解决方案


最后,我设法解决了我自己的问题。

我只需要在整个单词、前缀单词、后缀单词或前缀和后缀单词的搜索模式之前和之后添加“\s”。

    string pattern_anywordness_middle = @"(" + pattern_whole_line + "|"
                                            + @"(" + @"\s" + pattern_whole_word + @"\s" + @")" + "|"
                                            + @"(" + @"\s" + pattern_prefix + @"\s" + @")" + "|"
                                            + @"(" + @"\s" + pattern_suffix + @"\s" + @")" + "|"
                                            + @"(" + @"\s" + pattern_prefix_and_suffix + @"\s" + @")"
                                      + @")";

推荐阅读