Monday, February 22, 2016

C# Regex Positive Lookbehind

An example of positive lookbehind (Matches a group before the main expression without including it in the result):

            var str = @"System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.ApplicationException: MQException is caught. Reason code: 2033, completion code: 2. ---> CompCode: 2, Reason: 2033
                       --- End of inner exception stack trace ---
                       at Multiconn.ZZZ.Web.Services.Port.EndProcess(IAsyncResult asyncResult)
                       at Multiconn.ZZZ.Demo.K.M.E(IAsyncResult asyncResult, WsEsParams1Response& result1, MyType result3)
                       --- End of inner exception stack trace ---";

            Regex r = new Regex(@"(?<=Reason code: )\d+");

            foreach (Match m in r.Matches(str))
                Console.WriteLine(m.Value);

No comments:

Post a Comment