• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 609
  • Last Modified:

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:


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"


-- 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

  • 3
  • 2
1 Solution
string rp = @"\d{4}-\d{2}-\d{2}(.*?)\d{2}:\d{2}:\d{2}";
string fixedString = Regex.Replace(xmlString, rp, "$` $'");
Anurag ThakurTechnical ManagerCommented:
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");
firepolAuthor Commented:
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...
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Anurag ThakurTechnical ManagerCommented:
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
firepolAuthor Commented:
ragi0017: I need to keep the DateTime type, I can't and don't want to use strings... else I would have used a string from the beginning to make my life easier...

The regex was already ok. Just the way to replace the T was not ok. Here the working code I wrote myself:

        // Search for datetime values of the format 2004-08-22T00:00:00
        Regex rx = new Regex(@"(?<DATE>\d{4}-\d{2}-\d{2})(?<TIME>T\d{2}:\d{2}:\d{2})");

        MatchCollection matches = rx.Matches(xmlString);

        string fixedString = xmlString;

        foreach (Match match in matches)
            // Matched date with "T"
            string oldDate = match.Value;

            // Get rid of the T inside the matched Date
            string newDate = Regex.Replace(oldDate, @"T", " ");

            // Search the string for the oldDate and replace it with newDate
            fixedString = Regex.Replace(fixedString, oldDate, newDate);
Anurag ThakurTechnical ManagerCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now