-4

I have a deserialized xml which looks like this:

http://wklej.org/id/2869540/

How to write a regex to find and return only first date expression 23.12.2010, which may be another in every another xml documents.

I have never used regex and I don't even know how to write a pattern for it. Please help.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

2 Answers2

3

Don't use Regex for HTML/XML parsing. Use Html/Xml parser. Here is explain well why you should not use it.

RegEx match open tags except XHTML self-contained tags

Can you provide some examples of why it is hard to parse XML and HTML with a regex?

You can load the string in XDocument or XmlDocument and using linq to take whatever you need.

Here little example of how to do it:

string str =
@"<?xml version=""1.0""?>
<!-- comment at the root level -->
<Root>
    <Child>Content</Child>
</Root>";
XDocument doc = XDocument.Parse(str);

After that select needed nodes using linq and take the value. Here this question could help:

Finding element in XDocument?

Community
  • 1
  • 1
mybirthname
  • 17,949
  • 3
  • 31
  • 55
0

As suggested by the others, use a Html/Xml parser. If you really want to use regex, you can try this:

 string xml= "yourXMLString";
 string pattern = @"\d{1,2}\.\d{1,2}\.\d{4}"; //also matches dates like 1.3.2016. Use \d{2} to only match 01.03.2016
 Regex regEx = new Regex(pattern);

 Match m = regEx.Match(xml);   // m is the first match
 if (m.Success)
 {
    Console.WriteLine(m.Value); //prints the first found date
 }
eol
  • 23,236
  • 5
  • 46
  • 64
  • ok. Thank you. I have that problem that my program need to read all docs looks like this one. One has date like 01.01.2000 , other has date like 01/01/2000, other 01- 01- 2000, and other 01 - 01 - 2000. Don't know how to solve it with pattern. – Grzesiek Zimnoch Sep 26 '16 at 08:06