简体   繁体   中英

C# RegEx remove everyting before syncML xml payload of a line

The program i have created is parsing out log lines. The log lines also contain SYNCML data in request and response fashion.

I have the first part of the parser working correctly where it's parsing out the loglines with the sync ML data contained so just the request and response log lines are contained. Now I want to strip everything from that log line and just leave the syncML data. I am not really sure how I can do this.

This is what i have so far.

        Regex request = new Regex(@"request class SyncML");
        Regex response = new Regex(@"response class SyncML");

       string line;
        while ((line = sr.ReadLine()) != null)
             {
                 Match req = request.Match(line);
                 Match res = response.Match(line);

                 if (req.Success)
                 {
                     string v = req.Groups[1].Value;
                     Console.WriteLine(line);
                     Console.WriteLine("\t" + v);
                 }

                 if (res.Success)
                 {
                     string v = res.Groups[1].Value;
                     Console.WriteLine(line);
                     Console.WriteLine("\t" + v);
                 }
             }

So this works and all, but it the lines have all the other info from the program contained, such as date time and a whole lot of other stuff I don't care about. I just want the pure XML.. Every start of the xml start with

  <?xml 

So how do i remove everything before the < ? xml and keep the rest? Basically i just want the xml.

Does that make sense?

The log lines looks something like this:

    2015-10-08T10:15:01.383-0400 <bunch of other crap> request class SyncML: <?xml version="1.0  yadadada until the end.

It looks like it was easier then I thought.

 string s = line.Substring(line.IndexOf("<?xml "));

This removes everything infront of the xml payload and what's left is just the xml payLoad.

@jdweng your right Regex is not the tool, SubString is.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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