具有各种组合的正则表达式

regexp with various combinations

这是我正在寻找的模式 [stringData1|stringData2|OnlyNumericData1]stringData1 的最大大小限制为 10stringData2 的最大大小限制为 50stringData1、stringData2 和 OnlyNumericData1 中至少应有一个字符

案例 #1: ("Here is the test subject line [testData1|CS-SPL-GD-2014-01254|678] and [[test-1213-778]"); // Returns: [testData1|CS-SPL-GD-2014-01254|678]

案例 #2:"Here is the test subject line [testData1|1tes2345|678] and [resrtest-1213667-77448]"); // Returns: [testData1|1tes2345|678]

案例#3:("Here is the test subject line [test Data1|-12|iiiii|345-678] and [test|124413tryrtytry|77218]");// Returns: [test|124413tryrtytry|77218]

案例 #4:("Here is the test subject line [Hello Test|1w2345|678] and [Hellotest|122 2213|72278]");// Returns:return 没什么。因为数据

中有空白space

案例 #5:("Here is the test subject line [33|1d-d-dff=gg2345|test] and [teddddst|1|21433}|7788888]");// Returns: Returns [33|1d-d-dff=gg2345 |test] 第一次出现模式匹配

到目前为止我已经尝试了什么。

        var result = Regex.Match(subjectLine, @"\[\w{1,10}|\w{1,50}|[\d]+\]");
        if (result.Success)
        {

        }
        else
        {

        }

正在为此标准寻找 C# 正则表达式。


你的正则表达式有两个问题:

  1. 你必须转义管道符|
  2. \w 不匹配 -=。您可能想使用 \S.

结果:

\[\S{1,10}\|\S{1,50}\|\d+\]