繁体   English   中英

正则表达式模式失败

[英]Regex pattern failing

我正在尝试从字符串开头到具有转义序列"\\r\\n\\r\\n"的点查找子字符串,我的正则表达式为Regex completeCall = new Regex(@"^.+?\\r\\n\\r\\n", RegexOptions.Compiled); 只要您只有123\\r\\n\\r\\n类的字符串,它就很好用,但是一旦有了模式123\\r\\n 456\\r\\n\\r\\n该模式就不再匹配。

对我做错了什么建议吗?

Regex completeCall = new Regex(@"^.+?\r\n\r\n", RegexOptions.Compiled);
Regex junkLine = new Regex(@"^\D", RegexOptions.Compiled);
private void ClientThread()
{
    StringBuilder stringBuffer = new StringBuilder();
    (...)
    while(true)
    {
        (...)
        Match match = completeCall.Match(stringBuffer.ToString());
        while (Match.Success) //once stringBuffer has somthing like "123\r\n  456\r\n\r\n" Match.Success always returns false.
        {
            if (junkLine.IsMatch(match.Value))
            {
                   (...)
            }
            else
            {
                   (...)
            }
            stringBuffer.Remove(0, match.Length); // remove the processed string
            match = completeCall.Match(stringBuffer.ToString()); // check to see if more than 1 call happened while the thread was sleeping.
        }
        Thread.Sleep(1000);
    }

此处的编辑是导致其失败的数据(\\ r \\ n转换为实际的换行符)

691  25 2102   7:29     1:12      3585551234                                  A --Matches fine

692  27 2102   7:29     0:39      2155555432                                  A --Regex will not match this line when it comes up.
  *     2190            0:31                                               ABN  

693  28 2102   7:30     0:23      3055551212                                  A --never gets here because it is stuck on the previous line.

. 正则表达式中的换行符不匹配。 您需要指定RegexOptions.Singleline选项来解决此问题。

我以某种方式认为vfilby确实意味着该选项。 =)

该模式^.+?\\r\\n\\r\\n匹配字符串123\\r\\n\\r\\n因为123不包含换行符(默认情况下.不匹配\\n在.NET)。 要让您的模式也匹配123\\r\\n 456\\r\\n\\r\\n ,请启用DOT-ALL选项:

`(?s)^.+?\r\n\r\n`

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM