【问题标题】:C# RegEx: How to match a string inside the middle words only of a text line?C# RegEx:如何仅匹配文本行中间单词内的字符串?
【发布时间】:2019-09-02 01:08:53
【问题描述】:

使用 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”中找到匹配项。

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

【问题讨论】:

  • 我知道这很愚蠢,但为什么不干脆做 'string.Split()` 之类的事情,然后删除第一个和最后一个单词,进行匹配,然后在需要时将它们添加回来?
  • 当然这是我要做的第一件事,但这是一个接受条件(location_in_line,location_in_word,...)并返回一个 RegEx 模式的方法,然后我在其他地方使用它来运行包含 1000 行的文件。
  • 用正则表达式做pattern_empty_linepattern_whole_line 这样的事情是多余的。事实上,我没有看到有人试图对后者中的文本进行转义,因此可以有意或无意地将它用于您的程序中的cause problems
  • 完整的应用程序是开源的,因此请随时在github.com/heliwave/QuranCode 浏览整个 C# 代码。上面的代码摘录可以在字符串 BuildPattern(...) Server\Server.cs 中找到。如果您可以建议对代码进行改进,请也这样做。提前谢谢你。

标签: c# regex text-editor


【解决方案1】:

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

我只需要在整个单词、前缀单词、后缀单词或前缀和后缀单词的搜索模式之前和之后添加“\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" + @")"
                                      + @")";

【讨论】:

    猜你喜欢
    • 2022-11-19
    • 1970-01-01
    • 2015-06-11
    • 2010-10-24
    • 2014-05-02
    • 1970-01-01
    • 2018-05-12
    • 1970-01-01
    • 2017-05-09
    相关资源
    最近更新 更多