Regex pattern failing
I am trying a substring to find from the beginning of the string to the point that has the escape sequence "\r\n\r\n"
my regex is Regex completeCall = new Regex(@"^.+?\r\n\r\n", RegexOptions.Compiled);
it works great as long as you only have strings like 123\r\n\r\n
however once you have the pattern 123\r\n 456\r\n\r\n
the pattern no longer matches.
Any advice on what I am doing wrong?
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);
}
Edit here is the data that is causing it to fail (\r\n translated to real linebreaks)
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 stu开发者_开发问答ck on the previous line.
The .
in your Regex does not match line breaks. You need to specify the RegexOptions.Singleline
option to remedy that.
Somehow I think vfilby really meant this option. =)
The pattern ^.+?\r\n\r\n
matches the string 123\r\n\r\n
because 123
does not contain line breaks (by default, .
does not match \n
in .NET). To let your pattern also match 123\r\n 456\r\n\r\n
, enable the DOT-ALL option:
`(?s)^.+?\r\n\r\n`
精彩评论