簡體   English   中英

如何以開始模式查找字符串並包含模式

[英]How to find string with beginning pattern and contains pattern

我一直在研究此文章 ,嘗試將ac#regex放在一起進行以下操作: 查找一個字符串是否包含另一個以某些字母開頭並包含某些字符的字符串。

這是干草堆字符串的1個具體示例:

NOT SUPPCODE{900mm,1500mm} and IDU{true}

我需要發現haystack字符串是否包含NOT (最好不區分大小寫),后跟1個空格,然后緊跟一個不間斷的空格“ word”,該單詞包含以下3個字符(順序但不相鄰): {,} 換句話說,左/右花括號必須包含一個或多個逗號。 花括號內的空格很好 ,但SUPPCODE (在此示例中)和左花括號之間不得有空格。

我的干草堆示例實際上與該模式匹配,因為沒有一個NOT (不需要在字符串的開頭),后跟一個空格,然后是一系列包含左花括號,逗號,和右大括號。 這三個字符將不相鄰。

這是我根據上面提到的帖子整理的C#代碼,不適用於我:

public static bool ContainsRegex(string haystack, string startsWith, string contains) {
   var regex = new Regex("(?=.*" + contains + ")^" + startsWith);
   int matches = regex.Matches(haystack).Count;
   return matches > 0;
}

這樣稱呼:

bool isFound = ContainsRegex("NOT SUPPCODE{900mm,1500mm} and IDU{true}", "NOT ", "{,}");

這些字符串參數當然是動態的,並且在運行時總是不同的。

即使在應返回true的情況下(如上所示),我的函數也始終返回false。

相比之下,以下是一些負面測試字符串,應返回false:

SUPPCODE{900mm,1500mm} and IDU{true} // doesn't begin with NOT
STUFF SUPPCODE{900mm,1500mm} and IDU{true} // doesn't begin with NOT
NOT SUPPCODE{900mm} and IDU{true} // no comma between curly braces
NOT SUPPCODE,5,6900mm} and IDU{true} // no left curly brace
NOTSUPPCODE{900mm,1500mm} and IDU{true} // no space between NOT and SUPPCODE
NOT SUPPCODE {900mm,1500mm} and IDU{true} // space between SUPPCODE and left curly brace

我究竟做錯了什么?

您可以使用

public static bool ContainsRegex(string haystack, string startsWith, string contains) 
{
    var delims = contains.Select(x => x.ToString().Replace("\\", @"\\").Replace("-", @"\-").Replace("^", @"\^").Replace("]", @"\]")).ToList();
    var pat = $@"^{startsWith} \w+{Regex.Escape(contains.Substring(0,1))}[^{string.Concat(delims)}]*{Regex.Escape(contains.Substring(1,1))}[^{delims[0]}{delims[2]}]*{Regex.Escape(contains.Substring(2,1))}";
    // Console.WriteLine(pat); // => ^NOT \w+\{[^{,}]*,[^{}]*}
    return Regex.IsMatch(haystack, pat, RegexOptions.IgnoreCase);
}

這是一個例子

var strs = new[] { "SUPPCODE{900mm,1500mm} and IDU{true}",
            "STUFF SUPPCODE{900mm,1500mm} and IDU{true}",
            "NOT SUPPCODE{900mm} and IDU{true}",
            "NOT SUPPCODE,5,6900mm} and IDU{true}",
            "NOTSUPPCODE{900mm,1500mm} and IDU{true}",
            "NOT SUPPCODE {900mm,1500mm} and IDU{true}",
            "NOT SUPPCODE{900mm,1500mm} and IDU{true}"};
foreach (var s in strs)
    Console.WriteLine($"{s} => {ContainsRegex(s, "NOT", "{,}")}");

輸出:

SUPPCODE{900mm,1500mm} and IDU{true} => False
STUFF SUPPCODE{900mm,1500mm} and IDU{true} => False
NOT SUPPCODE{900mm} and IDU{true} => False
NOT SUPPCODE,5,6900mm} and IDU{true} => False
NOTSUPPCODE{900mm,1500mm} and IDU{true} => False
NOT SUPPCODE {900mm,1500mm} and IDU{true} => False
NOT SUPPCODE{900mm,1500mm} and IDU{true} => True

假定contains參數僅contains 3個字符:起始定界符是第一個,中間的是內部的強制性字符,然后第三個字符是尾隨的字符。

另請參見生成的regex演示

細節

  • ^ -字符串的開頭
  • NOT startsWith字符串
  • - 空間
  • \\w+ -1個以上的字符字符
  • \\{ -起始分隔符
  • [^{,}]* -除定界符外的0+個字符
  • , -中間必填字符
  • [^{}]* -除開始和結束定界符以外的0+個字符
  • } -尾部定界符。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM