正则表达式不捕获预期字符串

时间:2016-07-20 19:36:27

标签: c# regex

模式:

000[0-9]Text(.*)?000[0-9]Font
  

ISystem,Version = 2.0.0.0,Culture = neutral,   公钥= b77a5c561934e089
  -System.Collections.Specialized.ListDictionary头版本计数比较器
     System.Collections.IComparer
        0001 Text QSystem.Drawing,Version = 2.0.0.0,   Culture = neutral,PublicKeyToken = b03f5f7f11d50a3a
  0001Center System.Drawing.PointF x y \   =mÀÉ@ 0001Expansion System.Drawing.SizeF   宽度高度Ì6AûY? 0001Color
  System.Drawing.Color名称值knownColor状态ÿ
  0001Text YPARTIAL RELEASE in 09 / 2573,09 / 2835,09 / 1908,11 / 86,   11 / 1741,11 / 1803,11 / 2484,11 / 2765 0001Font
  System.Drawing.Font名称大小样式单位
  System.Drawing.FontStyle System.Drawing.GraphicsUnit
  Microsoft Sans Serif@AèÿÿÿSystem.Drawing.FontStylevalue__
  çÿÿÿSystem.Drawing.GraphicsUnitvalue__
  0001Charset 0001TextOrientation
  0002! 0002Center“#”)\> {Î@#
  $ 0002Expansion%& %)\ @Ãõ(?&'0002Color   ()(ÿ)* 0002Text + 12/452,   12/1629 ,, - 0002Font。 /。 0微软   Sans Serif@AÏÿÿÿèÿÿÿÎÿÿÿçÿÿÿ/ 3 0002Charset
  4 4 5 0002TextOrientation 6 6 7 0003
  9 9:0003中心; < ; ö(\¿...ËÀ< =
  0003扩展> ? > ......ë@? ? @ 0003Color A
  B A%B C 0003Text D满意度   IN 13/25 E E F 0003Font G H G I
  Microsoft Sans Serif@A¶ÿÿÿèÿÿÿμÿÿÿçÿÿÿHL
  0003Charset M M N 0003TextOrientation

我期待的是:

  • YPARTIAL RELEASE IN 09/2573, 09/2835, 09/2908, 11/86, 11/1741, 11/1803, 11/2484, 11/2765
  • 12/452, 12/1629 , , -
  • D SATISFACTION IN 13/25 E E F

在.NET正则表达式引擎中,它返回null

我在Expresso中得到了什么:

 YPARTIAL RELEASE IN  09/2573, 09/2835, 09/2908, 11/86,  11/1741, 11/1803, 11/2484, 11/2765                    0001Font                System.Drawing.Font     Name Size Style Unit      System.Drawing.FontStyle     System.Drawing.GraphicsUnit              Microsoft Sans Serif  @A èÿÿÿ System.Drawing.FontStyle     value__           çÿÿÿ System.Drawing.GraphicsUnit     value__                         0001Charset                          0001TextOrientation                          0002                    !    0002Center "    #    "       )\ >{ Î@ #        $    0002Expansion %    &    %       )\@Ãõ(? &        '    0002Color (    )    (           ÿ         )        *    0002Text +    12/452,  12/1629 ,    ,        -    0002Font .    /    .        0    Microsoft Sans Serif  @A Ïÿÿÿèÿÿÿ     Îÿÿÿçÿÿÿ     /        3    0002Charset       4    4        5    0002TextOrientation       6    6        7    0003      9    9        :    0003Center ;    <    ;       ö(\¿ …ËÀ <        =    0003Expansion >    ?    >       …ë @   ? ?        @    0003Color A    B    A                %    B        C    0003Text D    SATISFACTION  IN  13/25 E    E        F    

C#代码

Regex reg = new Regex(@"000\dText(.*)?000\dFont", RegexOptions.Multiline);
foreach (Match match in reg.Matches(test))
{
     if(match.Groups[1].Captures[0].Value.IndexOf("System") < 0)
        details += match.Groups[1].Captures[0].Value.Trim() + "\r\n";
}

1 个答案:

答案 0 :(得分:1)

使用

@"000[0-9]Text\b(.*?)000[0-9]Font\b"

请参阅regex demo

两点:

  • (.*)?是一种贪婪的匹配模式,匹配最后一个000 +数字,但可选(?,一个或零)。您需要更改为延迟点匹配.*?
  • Text后面应加上字边界,您可以匹配0001TextOrientation块。

enter image description here