Link to home
Start Free TrialLog in
Avatar of firepol
firepol

asked on

regular expression to replace what's Inside a match

Hello, I think the easiest is to show you what I get with my regex, and tell you what I'd like to achieve...

If I run the attached C# code snippet I get this result:

-- BEGIN

xmlString: DT_EFFECTIVE_FROM_UTC="2008-09-25T16:18:55" SOME_ATTRIBUTE="This T should Stay" DT_EFFECTIVE_UNTIL_UTC="2009-01-01T00:00:00" ANOTHER_ATTRIBUTE="Value For This Attribute"

fixedString: DT_EFFECTIVE_FROM_UTC="T" SOME_ATTRIBUTE="This T should Stay" DT_EFFECTIVE_UNTIL_UTC="T" ANOTHER_ATTRIBUTE="Value For This Attribute"

-- END

The regular expression matches the "T" inside a datetime of the following format: 2008-09-25T16:18:55.

What I want to achieve, is to get rid of the "T" inside the datetime. So I'd like to get this:

fixedString: DT_EFFECTIVE_FROM_UTC="2008-09-25 16:18:55" SOME_ATTRIBUTE="This T should Stay" DT_EFFECTIVE_UNTIL_UTC="2009-01-01 00:00:00" ANOTHER_ATTRIBUTE="Value For This Attribute"

Please give me the correct regular expression to use, or the corrections to my code to obtain it.

Thank you.
//Goal: get rid of the "T" inside the datetime
 
string xmlString = "DT_EFFECTIVE_FROM_UTC=\"2008-09-25T16:18:55\" SOME_ATTRIBUTE=\"This T should Stay\" DT_EFFECTIVE_UNTIL_UTC=\"2009-01-01T00:00:00\" ANOTHER_ATTRIBUTE=\"Value For This Attribute\"";
 
// Search for datetime values of the format 2004-08-22T00:00:00
string rp = @"\d{4}-\d{2}-\d{2}(.*?)\d{2}:\d{2}:\d{2}";
 
// TODO: replace just the "T" with a blank space.
// The following doesn't work as I want. It replaces what is OUTSIDE the T... I want to get rid of the T, not of the date.
string fixedString = Regex.Replace(xmlString, rp, "$1");
 
// I use it from an aspx page, thus I use Response.Write
Response.Write("<pre>" + xmlString + "\r\n\r\n" + fixedString + "</pre>");

Open in new window

Avatar of ahoffmann
ahoffmann
Flag of Germany image

string rp = @"\d{4}-\d{2}-\d{2}(.*?)\d{2}:\d{2}:\d{2}";
string fixedString = Regex.Replace(xmlString, rp, "$` $'");
if possible i will fix the source of the problem and not go for a regular expression fix
the source of the data is DateTime.ToString() function
just use DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
Avatar of firepol
firepol

ASKER

I copied exactly your code:

string rp = @"\d{4}-\d{2}-\d{2}(.*?)\d{2}:\d{2}:\d{2}";
string fixedString = Regex.Replace(xmlString, rp, "$` $'");

and I obtain:

fixedString: DT_EFFECTIVE_FROM_UTC="DT_EFFECTIVE_FROM_UTC=" " SOME_ATTRIBUTE="This T should Stay" DT_EFFECTIVE_UNTIL_UTC="2009-01-01T00:00:00" ANOTHER_ATTRIBUTE="Value For This Attribute"" SOME_ATTRIBUTE="This T should Stay" DT_EFFECTIVE_UNTIL_UTC="DT_EFFECTIVE_FROM_UTC="2008-09-25T16:18:55" SOME_ATTRIBUTE="This T should Stay" DT_EFFECTIVE_UNTIL_UTC=" " ANOTHER_ATTRIBUTE="Value For This Attribute"" ANOTHER_ATTRIBUTE="Value For This Attribute"

It doesn't work. The fixedString seems to double length and the T is still there...

Maybe I should do a foreach and for every matched occurrence, I need to do a simple replace of the "T" with " "... ?

I can't eliminate the problem from the source (I've already tried it) because the DateTime object, when SERIALIZED into XML becomes by default a string with this format: 2009-01-01T00:00:00. A manual replace is needed as I'm sending the XML to a third party application that does not support the "T" in the dateTimeFormat...
Actually I am also using serialized objects to be passed as XML to my SQL Stored Procedures
Actually what happens that the datetime serialization is what we are trying to fix
but if i create a public property which returns that date as a string it will solve the problem

public stirng DateString
{
   get { return date.ToString("yyyy-MM-dd hh:mm:ss"); }
   set { throw new InvalidOperationException (); }
}

-- something like this and it will return the datetime without the "T" in it
ASKER CERTIFIED SOLUTION
Avatar of firepol
firepol

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
totally understood
even i could have done that but i took the call of doing the string because first its being serialized to an XML which is already a string so didnt see much harm

Also after looking at your code and the amount of extra code and processing i think that using a string was a better option for me - lower code to maintain

Thanks anyways for giving me the regex approach - who knows might come handy later