Solved

regular expression to replace what's Inside a match

Posted on 2008-10-08
6
564 Views
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:

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

0
Comment
Question by:firepol
  • 3
  • 2
6 Comments
 
LVL 51

Expert Comment

by:ahoffmann
ID: 22667488
string rp = @"\d{4}-\d{2}-\d{2}(.*?)\d{2}:\d{2}:\d{2}";
string fixedString = Regex.Replace(xmlString, rp, "$` $'");
0
 
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");
0
 
LVL 2

Author Comment

by:firepol
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...
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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
0
 
LVL 2

Accepted Solution

by:
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);
        }
0
 
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
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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…

776 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