0

I have an xml in the form of a string. Let's say that the element name is TestDate and the date is the form YYYY-MM-DD. So in other words <TestDate>1950-03-31</TestDate>.

How do I find this.

Lets say I have string xml;

xml contains the element TestDate. I want to get that using a regular expression.

string regularExpression = @"";
Regex regex = new Regex(regularExpression , RegexOptions.Compiled);

What should be going in for regular expression.

1
  • Your syntax and object references imply you are using C#, but please specify what language you are using in a tag or the question or both Commented Nov 3, 2011 at 15:53

2 Answers 2

1

This should match your Date: [0-9]{4}-[0-9]{2}-[0-9]{2}

if you can use Perl-RegEx-Syntax, you can use \d instead the [0-9]

Sign up to request clarification or add additional context in comments.

1 Comment

\d works for almost every flavor of regex, but as specified in a comment to another answer, the expression should match the whole tag, not just a date
0

If you want search data in that element only than itshould be

<TestDate>[0-9]{4}-[0-9]{2}-[0-9]{2}</TestData>

Another aproach is to use XPath. Load xml string in XmlDocument and extract all similar nodes. Below code will return all TestDate Nodes.

XmlNode[] nodes = doc.SelectNodes("//TestDate");

2 Comments

I want to get the whole element and also its content.
Your regex specifies matching of unbalanced tags (if used verbatim), due to a typo, I believe.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.