Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.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# 使用正则表达式计算字符串_C#_Regex - Fatal编程技术网

C# 使用正则表达式计算字符串

C# 使用正则表达式计算字符串,c#,regex,C#,Regex,我正在使用Regex.IsMatch()计算单词列表 如果对以下字符串求值,Regex应返回true: -海洛斯特 -你好 -阿切洛 如果对以下字符串求值,Regex应返回false: -地狱世界 -世界测试 -ABC世界 基本上,如果在字符串中找到单词“hello”,并且如果在字符串中未找到单词“world”,我希望它返回true 产生这种结果的正则表达式是什么?使用表达式 (?<!world.*)hello(?!.*world) (? (?是一个否定的环视。pos仅在前面没有前缀的

我正在使用
Regex.IsMatch()
计算单词列表

如果对以下字符串求值,
Regex
应返回
true
: -海洛斯特 -你好 -阿切洛

如果对以下字符串求值,
Regex
应返回false: -地狱世界 -世界测试 -ABC世界

基本上,如果在字符串中找到单词
“hello”
,并且如果在字符串中未找到单词
“world”
,我希望它返回
true

产生这种结果的
正则表达式是什么?

使用表达式

(?<!world.*)hello(?!.*world)
(?

(?是一个否定的环视。
pos
仅在前面没有
前缀的情况下匹配


pos(?!suffix)
只有在未被
suffix

取代的情况下才匹配。为什么不使用string.contains?@user1295450-哇!为什么这么复杂的答案被认为是最好的答案?使用可变长度负查找是最差的性能解决方案。而有两个负查找是一个魔鬼解决方案(对不起Olivier!)如果性能是个问题,为什么要使用正则表达式呢?Abe Miessler删除的答案
myString.Contains(“hello”)和&!myString.Contains(“world”);
可能快得多。OP要求使用正则表达式,所以我们应该给他最好的正则表达式解决方案