regular expression to replace what's Inside a match

Posted on 2008-10-08
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...
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

I have been reconstructing a PHP-based application that has grown into a full blown interface system over the last ten years by a developer that has now gone into business for himself building websites. I am not incredibly fond of writing PHP code o…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now