【问题标题】:Word boundaries not matching when the word starts or ends with special character like square brackets当单词以方括号等特殊字符开头或结尾时,单词边界不匹配
【发布时间】:2019-11-19 17:09:54
【问题描述】:

我想用另一个数字替换作为方括号的字符串。我正在使用正则表达式替换方法。

示例输入:

这是[测试]版本。

所需输出(用 1.0 替换“[test]”):

这是 1.0 版本。

现在正则表达式没有替换特殊字符。以下是我尝试过的代码:

 string input= "This is [test] version of application.";

 string stringtoFind = string.Format(@"\b{0}\b", "[test]");

 Console.WriteLine(Regex.Replace(input, stringtoFind, "1.0"));

input 和 stringtoFind 变量中可能有任何特殊字符。

【问题讨论】:

  • 在正则表达式中转义 [ 和 ] 字符
  • 您不需要正则表达式。只需input.Replace("[test]", "1.0")
  • 检查我修改后的答案
  • 不确定是否是 OP 在进行否决,但似乎大多数回复都没有考虑到问题的最后一行:“输入和 stringtoFind 变量中可能有任何特殊字符。 "
  • 您只是想替换 [Test] 或方括号中的任何参数吗?

标签: c# regex word-boundary word-boundaries


【解决方案1】:
\] // Matches the ]
\[ // Matches the [

这是一份备忘单,您将来可以使用https://www.rexegg.com/regex-quickstart.html#morechars

string input = "This is [test] version of application.";

string stringtoFind = string.Format(@"\[[^]]+\]", "[test]");

Console.WriteLine(Regex.Replace(input, stringtoFind, "1.0"));

Console.ReadKey();

https://www.regexplanet.com/share/index.html?share=yyyyujzkvyr => 演示

【讨论】:

  • 代码转储是怎么回事?你能解释一下你的答案吗?
  • 您能否在答案中解释您的答案?请不要链接到外部网站,除非您已将相关内容放在答案中。
  • @Enigmativity 如上所述,\]与输入中的] 相匹配,等等......我应该解释更多,外部站点是可以使用的备忘单。
  • 在您的正则表达式中,除了\[\],还有更多内容。您应该解释得足够多,以便 OP 可以理解您的解决方案 - 您已经引入了新的正则表达式,所以您应该解释一切。外部链接仅用于支持您答案中的现有内容。
【解决方案2】:

你必须在这里考虑两件事:

  • 特殊字符必须使用文字 \ 符号进行转义,当您将动态文字文本作为变量传递给正则表达式时,最好使用 Regex.Escape 方法完成
  • 不可能依赖单词边界\b,因为此构造的含义取决于直接上下文。

您可以使用Regex.Escape 和明确的单词边界(?<!\w)(?!\w)

string input= "This is [test] version of application.";
string key = "[test]";
string stringtoFind = $@"(?<!\w){Regex.Escape(key)}(?!\w)";
Console.WriteLine(Regex.Replace(input, stringtoFind, "1.0"));

请注意,如果您想替换包含空格的键字符串,请使用

string stringtoFind = $@"(?<!\S){Regex.Escape(key)}(?!\S)";
                         ^^^^^^                    ^^^^^

【讨论】:

  • 如果这会影响带有特殊符号的开头和结尾单词,您认为在某些情况下有效的解决方法是在匹配后在字符串的末尾/开头添加一些符号可以删除(只要您删除它们而不是字符串的其他部分)?
  • @gotqn 我总是尽量避免修改输入字符串,因为在将“单词”与 .NET 正则表达式匹配时它不会提供任何额外的灵活性。唯一的麻烦是定义“词”。一旦你知道你的“单词”可能包含哪些字符,你就可以开始研究边界了。例如,您可以将abc 视为下划线内的单词(因此,单词只能由字母和数字组成)。然后你将使用$@"(?&lt;![^\W_]){Regex.Escape(key)}(?![^\W_])"
【解决方案3】:

我的猜测是这个简单的表达式可能会起作用:

\[[^]]+\]

测试

using System;
using System.Text.RegularExpressions;

public class Example
{
    public static void Main()
    {
        string pattern = @"\[[^]]+\]";
        string substitution = @"1.0";
        string input = @"This is [test] version";
        RegexOptions options = RegexOptions.Multiline;

        Regex regex = new Regex(pattern, options);
        string result = regex.Replace(input, substitution);
    }
}

表达式在this demo 的右上角进行了解释,如果您想探索/简化/修改它,在this link 中,您可以逐步观察它如何与一些示例输入匹配,如果你喜欢。

【讨论】:

    【解决方案4】:

    在我看来,这与您所要求的最接近:

    string input = "This is [test] version of application.";
    
    string stringtoFind = Regex.Escape("[test]");
    
    Console.WriteLine(Regex.Replace(input, stringtoFind, "1.0"));
    

    输出This is 1.0 version of application.

    但是,在这种情况下,只需这样做就足够了:

    string input = "This is [test] version of application.";
    
    Console.WriteLine(input.Replace("[test]", "1.0"));
    

    它做同样的事情。

    【讨论】:

      【解决方案5】:

      您应该转义括号并删除\b

       string input= "This is [test] version of application.";
      
       string stringtoFind = string.Format("{0}", @"\[test\]");
      
       Console.WriteLine(Regex.Replace(input, stringtoFind, "1.0"));
      

      输出

      This is 1.0 version of application.
      

      重要提示

      \b 确实 NOT 匹配空格。 \b 匹配单词开头或结尾的空字符串。也许您正在寻找\s

      【讨论】:

        最近更新 更多