使用正则表达式和 C# 识别另一个字符串中不存在的字符串

Identify the string that does not exists in another string using regex and C#

我正在尝试捕获不包含在另一个字符串中的字符串。

string searchedString = " This is my search string";
string subsetofSearchedString = "This is my";

我的输出应该是 "Search string"。我只想使用正则表达式,这样我就可以处理复杂的字符串。

以下是我目前试过的代码,但没有成功。

 Match match = new Regex(subsetofSearchedString ).Match(searchedString );
 if (!string.IsNullOrWhiteSpace(match.Value))
 {
      UnmatchedString= UnmatchedString.Replace(match.Value, string.Empty);
 }

更新:以上代码不适用于以下文本。

text1 = 'Property Damage (2015 ACURA)' Exposure Added Automatically for IP:Claimant DriverLoss Reserve Line :Property DamageReserve Amount $ : STATIP Role(s): Owner, DriverExposure Owner :Jaimee Watson_csr Author:
text2 = 'Property Damage (2015 ACURA)' Exposure Added Automatically for IP:Claimant DriverLoss Reserve Line :Property DamageReserve Amount $ : STATIP Role(s): Owner, Driver

Match match = new Regex(text2).Match(text1);

您可以使用 Regex.Split:

var ans = Regex.Split(searchedString, subsetofSearchedString);

如果您希望答案是减去子集的单个字符串,您可以加入它:

var ansjoined = String.Join("", ans);

替换为 String.Empty 也可以:

var ans = Regex.Replace(searchedString, subsetOfSearchedString, String.Empty);

答案:

正则表达式对我不起作用,因为我的字符串中存在元字符。 Regex.Escape 没有帮助我进行比较。

String Contains 在这里非常有效

if (text1.Contains(text2))
   {  
      status = TestResult.Pass;
      text1= text1.Replace(text2, string.Empty);
   }