regular expression to replace what's Inside a match

Posted on 2008-10-08
Medium Priority
Last Modified: 2008-10-13
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

Question by:firepol
  • 3
  • 2
LVL 51

Expert Comment

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

Expert Comment

by:Anurag Thakur
ID: 22667685
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");

Author Comment

ID: 22668320
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 new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

LVL 26

Expert Comment

by:Anurag Thakur
ID: 22668637
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

Accepted Solution

firepol earned 0 total points
ID: 22670187
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);
LVL 26

Expert Comment

by:Anurag Thakur
ID: 22670267
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

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

597 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question