【发布时间】:2014-04-09 09:35:30
【问题描述】:
我将使用简单的代码来描述我的情况。 例如,以下是代码:
using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern = @"\b(?!non)\w+\b";
string input = "Nonsense is not always non-functional.";
foreach (Match match in Regex.Matches(input, pattern, RegexOptions.IgnoreCase))
Console.WriteLine(match.Value);
}
}
现在,我想用用户输入替换“非”。假设它被称为“UserInput”,并且代码是为获取用户输入而编写的。我想这样做,但存在错误:
string pattern = @"\b(?!{0})\w+\b", UserInput;
有没有办法用用户输入替换正则表达式模式中的“非”?
【问题讨论】:
标签: c# regex pattern-matching