REGEX to change multiple XML Date Values

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

In the example below, I'm trying to simply find all the date values in an XML
document (the XML is a string in this example) and then add an upper case Z
between the last digit and the closing '<' character. My strSearch is ok,
but my strReplace doesn't seem to do anything.

Anyone know how to get my desired result?

Thanks,

Paul
-------------------------------------------------------------

string saTests1 =
"<periodOfReport>2006-08-08</periodOfReport><transactionDate><value>2006-07-01</value></transactionDate><signatureDate>2006-09-01</signatureDate>";
string strSearch =
">(?<Year>(?:\\d{4}))-(?<Month>\\d{2})-(?<Day>\\d{2})<";
string strReplace = ">${year}-${month}-${day}Z<";

Regex.Replace( saTests1, strSearch, strReplace );

Desired output:
"<periodOfReport>2006-08-08Z</periodOfReport><transactionDate><value>2006-07-01Z</value></transactionDate><signatureDate>2006-09-01Z</signatureDate>";
 
a: you didn't name the sections, so $year etc don't exist
b: you need to capture the returned string

string strReplace = ">${1}-${2}-${3}Z<";
string result = Regex.Replace(saTests1, strSearch, strReplace);

Marc
 
It looks to me like you have named them too... (?<NAME>etc...)

The only thing I can see is that your names start with uppercase letters in
the regex, but not so in the Replace string - is it case sensitive I wonder?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top