Using signs while matching XML is confusing. So your expression could look like this: like this: latter version is better for our purpose. You can name a group by or ‘name’ syntax and reference it by using k or k’name’. If regex is complex it may be a good idea to ditch numbered references and use named references instead. Remember the (\w ) group? This group has number 1 and by using \1 syntax we are referencing the text matched by this group. matches XML close tag where element's name is provided with \1 backreference. String call = regex.Match(logLine).Value Regex regex = new Regex(pattern, RegexOptions.IgnorePatternWhitespace) Here’s the same regex with comments ( RegexOptions.IgnorePatternWhitespace is required to process expression commented this way): string pattern = # Backreference to opening tag name" If this regex is not entirely clear to you - read on, you will need to use something similar sooner or later. It may not be perfect but proved really helpful in log analysis. This short regular expressions has a couple of interesting parts. Getting to the proper information was quite easy thanks to Regex class: Regex regex = new call = regex.Match(logLine).Value call’s root element name may also appear in the “Result” section.there will be no line brakes in call’s data.call’s root element name will contain only alphanumerical chars or underscore This page provides an overall cheat sheet of all the capabilities of RegExp syntax by aggregating the content of the articles in the RegExp guide.call’s XML will be logged after “Call:” text on the beginning of line.When it comes to log, some things were certain: NET 3.5 or later is to invoke ToString method on XElement object: var xml = .Parse(someUglyXmlString) Quick tip: super-easy way to get such nicely formatted XML in. I was interested in XML data of the call: Here’s a sample log line (simplified, real log was way more complicated but it doesn’t matter for this post): Call:123 Result:John Smith Recently, I wanted to extract calls to external system from log files and do some LINQ to XML processing on obtained data.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |