Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.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#_.net_Regex - Fatal编程技术网

C# 我怎么才能拿到这个?!regex操作员是否正常工作?

C# 我怎么才能拿到这个?!regex操作员是否正常工作?,c#,.net,regex,C#,.net,Regex,我有一个搜索字符串,并使用正则表达式来查找不等于它的所有内容 i、 像这样 string searchStr = SearchStr; searchStr = System.Text.RegularExpressions.Regex.Escape(searchStr). Replace(@"\*", ".+"). Replace(@"\?", "."); //if (searchStr.StartsWith("!"))//(?!hede).) // searchStr =

我有一个搜索字符串,并使用正则表达式来查找不等于它的所有内容

i、 像这样

string searchStr = SearchStr;
searchStr = System.Text.RegularExpressions.Regex.Escape(searchStr).
    Replace(@"\*", ".+").
    Replace(@"\?", ".");
//if (searchStr.StartsWith("!"))//(?!hede).)
//    searchStr = "?!" + searchStr.Substring(1);

然后我执行Regex.isMatch,但我无法获得
要执行以下模式,有什么想法吗?

如果要搜索不等于
某物的单词,请尝试:

\b(?!something\b)\w+\b
要以编程方式构建它,请执行以下操作:

String.Format(@"\b(?!{0}\b)\w+\b", Regex.Escape(negativeSearchStr))
说明:

\b\w+\b
这是单词的模式

(?!something) 
是单词不以某物开头的否定前瞻。最后:

(?!something\b)

如果我理解正确的话,你就不能仅仅附加一个词?!一开始,你需要建立一个消极的前瞻组,根据你搜索的内容/方式,你希望锚定它。根据您所做的工作,类似的操作可能会起作用:

searchStr = "^(?!" + searchStr.Substring(1) + "$).*";
不过,否定正则表达式结果可能更容易:

Regex r = new Regex(..);
Match m = r.Match(someText);
if (m.Success != searchStr.StartsWith("!")) {
  // matches!
} 

.StartsWith(!”
false时,您希望正则表达式成功(返回true),反之亦然。

如果您希望避免以术语开头的字符串,则将使用lookback。Lookbehinds和lookaheads应该用括号括起来。现在,如果不添加括号,似乎只是在进行文字搜索,就像在搜索一个实际上包含问号和感叹号的字符串一样

假设我们有字符串“abc”和“bbc”,我们想找到不以a开头的字符串。您的lookback正则表达式将如下所示

(?<!a)b

(?对于一个开始的lookaheads和lookbehinds需要用括号括起来,例如
(?!dontcontainme)
谢谢你说刚刚反转的结果让我很高兴,bool invertSearch=searchStr.StartsWith(!”);if(invertSearch)searchStr=searchStr.Substring(1);if(Regex.IsMatch)(nd.Text、searchStr、RegexOptions.IgnoreCase)^invertSearch)