0

This is just a question out of curiosity - I was looking at this question and I'm wondering how you could go about making both needed replacements in one Regex Replace.

So, the situation is that we have the following input:

8.11.2012 16:15:10
21.11.2012 15:00:54
11.11.2012 0:24:24
8.11.2012 16:06:53
9.11.2012 0:49:37

And want to create the following output:

08_11_2012_16_15_10
21_11_2012_15_00_54
11_11_2012_00_24_24
08_11_2012_16_06_53
09_11_2012_00_49_37

In other words:

  1. Replace any single digits in time/date part of the string with "0" & Single character. The solution posted works great for that: Replace \b(\d)\b with 0$1
  2. Replace any separator with "_" And that would be simply replacing [\s\:.] with _

My question is whether there was any way to do BOTH replacements in a single Regex or if it's absolutely necessary to do 2 replace iterations?

What would that look like??

Just trying to learn here - Thanks!!

Also, even though this question was asked for Notepad++, I use .Net (C# / VB).

5
  • 1
    You start out with 5 inputs but only have 4 outputs. Is that a typo? You seem to be missing 09_11_2012_00_49_37 Commented Jun 17, 2013 at 15:53
  • 3
    FYI: If this is your specific need in .NET, you can do all this with (C# code): DateTime.ParseExact(@"d\/M\/yyyy H\:mm\:ss") and DateTime.ToString(@"dd\_MM\_yyyy\_HH\_mm\_ss") - rather than regular expressions. Commented Jun 17, 2013 at 15:54
  • @Joe - That is actually a really cool solution, but this was more of a question about how to do 2 regex replaces in a single statment and that was just the example I had at hand... Doesn't stop your solution from being a REALLY good one, though!! Commented Jun 17, 2013 at 15:56
  • 1
    @JohnBustos I was wrong and deleted the comment. Commented Jun 17, 2013 at 15:57
  • Thanks for catching that @Dgrin91 - You are 100% correct. I updated the question Commented Jun 17, 2013 at 15:59

3 Answers 3

2

You'd need to do them as 2 separate replaces.

A regex replace says find this match & replace it with this value. What you replace it with can include reference to the original match, and whilst you might be able to come up with a complicated pattern to match & replace both things, it just makes your expression more complicated and less likely to actually work.

Sign up to request clarification or add additional context in comments.

Comments

1

In C# you can do it in one iteration using delegate:

String result = Regex.Replace(input, @"(\d{1,2})\.(\d{1,2}).(\d{4}) (\d{1,2}):(\d{1,2}):(\d{1,2})",
    delegate(Match match)
    {
        string[] array = { match.Groups[1].Value.PadLeft(2, '0'), match.Groups[2].Value.PadLeft(2, '0'), match.Groups[3].Value.PadLeft(2, '0'), match.Groups[4].Value.PadLeft(2, '0'), match.Groups[5].Value.PadLeft(2, '0'), match.Groups[6].Value.PadLeft(2, '0')};
        return String.Join("_", array);
    });

To allow different separators use [\s\:.] instead of \.

Comments

1

You need to do 2 replacements.

The problem is that there is no conditional replacement-string and if you want a "0" to appear, "0" definitely needs to be part of your replacement-string.

If you had only one replacement, namely "_" (instead of "0" or "_" or both) resulting in: "_8_16_45__0_16_43", then there will be a way to do this :)

My 2 cents.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.