Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.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#RegEx:如何仅在文本行的中间单词内匹配字符串?_C#_Regex_Text Editor - Fatal编程技术网

C#RegEx:如何仅在文本行的中间单词内匹配字符串?

C#RegEx:如何仅在文本行的中间单词内匹配字符串?,c#,regex,text-editor,C#,Regex,Text Editor,使用C#RegEx,我只需要在文本行“123xxx123 123xxx xxx123xxx xxx123 123xxx xxx123xxx xxx123 123xxx123”的中间单词中匹配字符串“123” 它应该只匹配内部的“123”,而不是第一个字或最后一个字: “123xxx123[123][123]xxx xxx[123]xxx xxx[123]123xxx123” 我试着消极地向前看/向后看,但没有用 基本上,我需要支持一个Find实用程序,该实用程序具有查找匹配项(可以是多个单词)的

使用C#RegEx,我只需要在文本行“123xxx123 123xxx xxx123xxx xxx123 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 + @")";
string pattern_empty_line=@”(“+@”^$“+@”);
字符串模式整行=@“(“+@”^“+文本+@”$“+@”);
字符串模式整字=@“(“++”\b“+text++”\b“++”);
字符串模式_前缀=@“(“++”\S+?“+text++”);
字符串模式_后缀=@“(“+text++”\S+?“++”);
字符串模式_前缀_和_后缀=@“(“++”\S+?“+text++”\S+?“++”);
//任何措辞
字符串模式_anywordness_start=@”(“+模式_整行+”)
+“(“++”^“+模式整个词++”)“++”
+@“(“+@”^“+模式前缀+@”)“+”|”
+@“(“+@”^“+模式后缀+@”)“+”|”
+@“(“+@”^“+模式前缀和后缀+@”)
+ @")";
字符串模式_anywordness_end=@“(“+pattern_整行+”)
+“(“+pattern_-whole_-word++”$“++”)“+”|”
+@“(“+pattern_前缀+@“$”++”)“+”|”
+@“(“+pattern_后缀+@”$“++”)“+”|”
+@“(“+pattern_prefix_和_后缀+@”$“++”)”
+ @")";
字符串模式“\u anywordness\u not\u middle=@”(“+pattern\u整行+”)“+pattern\u anywordness\u start+”“+pattern\u anywordness\u end+”);
字符串模式\u anywordness\u middle=@“(“++”\b“++”.*“+text++”.*“++”\b“++”);
字符串模式_anywordness_anywhere=@”(“+text+@”);
//词性
字符串模式_partword_start=@“(“+pattern_prefix+”|“++”^“+pattern_prefix_和_后缀+@”);

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

我只需要在搜索模式之前和之后添加“\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" + @")"
                                      + @")";

我知道这很愚蠢,但为什么不干脆做一些类似“string.Split()`的事情,然后删除第一个和最后一个单词,进行匹配,然后在需要时再添加它们呢?当然,这是我要做的第一件事,但这是一个需要条件的方法(location_in_line,location_in_word,…)然后返回一个正则表达式模式,我在别处使用它来运行一个有1000行的文件。使用正则表达式执行
pattern\u empty\u line
pattern\u whole\u line
之类的操作是过分的。事实上,我没有看到有人试图在后者中转义文本,以便有意或无意地将其用于您的程序。完整应用tion是开源的,因此请在浏览整个C代码。上述代码摘录可在string BuildPattern(…)Server\Server.cs中找到。如果您可以对代码提出改进建议,请也这样做。提前感谢您。