Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I would like to replace this string 2000-12-13T13:59:59+12:00 to become 2000-12-13 13:59:59

Is that possible?, I can't use the common replace is because this value is mix together with other string/message, so I need to search the pattern to replace.

share|improve this question
    
Can you provide more examples of your work, if they are the same as the one you gave. We can archive it easily by writing our own string parser :) –  nXqd May 30 '12 at 8:04

3 Answers 3

up vote 0 down vote accepted
MessageBox.Show(Regex.Replace(
    @"2000-12-13T13:59:59+12:00",
    @"\b(\d{4}-\d{2}-\d{2})T(\d{2}:\d{2}:\d{2})[+-]\d{2}:\d{2}\b",
    @"$1 $2"
));

upd: added word boundaries around the pattern, some explanation and some links

\b                - word boundary (not to match "12000-12-13T..." or "X2000-12-13T..." etc, but still to match "(2000-12-13T..." and the like, optional)
(                 - start first capturing ($1)
\d{4}-\d{2}-\d{2} - date; 4 digits, dash, 2 digits, dash, 2 digits (\d for digit, {N} for exactly N of them)
)                 - end first capturing ($1)
T                 - literal "T"
(                 - start second capturing ($2)
\d{2}:\d{2}:\d{2} - time; 2 digits, colon, 2 digits, colon, 2 digits
)                 - end second capturing ($2)
[+-]              - utc offset sign; any of literal "+" or "-" (be careful with "-" inside [], if between other characters it defines range, like [a-z])
\d{2}:\d{2}       - utc offset; 2 digits, colon, 2 digits
\b                - word boundary (not to match "...+12:000" or "...+12:00a" etc, optional)

Resources: on regex in general, on .net, on C# in particular, simple tool for testing

share|improve this answer
    
Thanks! It's work! Could you direct me to a more complete regular expression reference as i search around and did not see can replace with $1 $2. –  tsohtan May 30 '12 at 23:52
    
Added some detail. I hope this helps. –  Eugene Ryabtsev May 31 '12 at 3:53

Using the regex

(.*)\s(\d{4}\-\d{2}\-\d{2})T(\d{2}:\d{2}:\d{2})\+\d{2}:\d{2}\s(.*)

and the following code

var regexPattern = @"(.*)\s(\d{4}\-\d{2}\-\d{2})T(\d{2}:\d{2}:\d{2})\+\d{2}:\d{2}\s(.*)";

        var dateString = "Some text here 2000-12-13T13:59:59+12:00 And some more text here";

        var formattedString = Regex.Replace(dateString, regexPattern, "$2 $3", RegexOptions.IgnoreCase);

will output

2000-12-13 13:59:59

The regex is reasonably strict, so you should be able to grab it from the other text, as long as it's always in that format - if the format changes at all (single digits in times or dates) then it won't match, but it's hard to say if that's a problem or not without context. It also assumes that the pattern only appears once in the text, and that it has a preceding and trailing space.

share|improve this answer
    
This uses spaces as separators and will not work with semicolons, brackets etc. Also, this will replace all the string, not only the time, which is often NOT the desired result. –  Eugene Ryabtsev May 31 '12 at 4:00

Try this

\+\d{2}:\d{2}$

code

string resultString = null;
try {
    resultString = Regex.Replace(subjectString, @"\+\d{2}:\d{2}$", "$${retain}", RegexOptions.IgnoreCase);
} catch (ArgumentException ex) {
    // Syntax error in the regular expression
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.