0

I have a collection of rows where the DateTime is not correct.

Is it possible to correct them with Regex.Replace?

    Wed2,8-Jul-21 64 53,0 57 
    Thu2, 9-Jul-21 73 60,0 48
    Fri, 30-Jul-21 86 70,0 36
    Sat,31-Jul-21 84 69,0 38
    Sun0, 1-Aug-21 89 73,0 33
    Mon0,2-Aug-21 98 80,0 24 
    Tue0, 3-Aug-21 91 75,0 31
    Wed0,4-Aug-21 92 75,0 30
    foreach (var row in rows)
    {
        Regex rg = new Regex([pattern]);
        rg.Replace(row, [replacement]);
    }

The wrong data is in the dateTime value; see below:

This is not correct: Wed2,8-Jul-21

The correct value is: Wed,28-Jul-21

Also, This is not correct: Thu2, 9-Jul-21

The correct value is: Thu,29-Jul-21

3
  • 1
    Please show what the "correct" data would be. Commented Aug 5, 2021 at 13:18
  • 1
    Surely it's possible. But which lines are not correct and what are the desired outcomes? Commented Aug 5, 2021 at 13:18
  • You could match index 3 and 5 of the string and check if they're numbers or white-space, and replace them with an empty string, either with regex or string manipulation. I recommend learning regex and reading stackoverflow.com/help/how-to-ask, as the question you've proposed is lacking in research Commented Aug 5, 2021 at 13:58

2 Answers 2

2

If the only problem is transposed characters, then do this for your pattern:

(Mon|Tue|Wed|Thu|Fri|Sat|Sun)(\d), *(\d)

Replace with

$1, $2$3
Sign up to request clarification or add additional context in comments.

3 Comments

@Chriss Maurer Thanks, but the main issue is on the first example. pls look at my comment on Good Night Nerd Pride. It is not working.
@Chris Maurer I don't know if it's mandatory to not have space or not, but with your current example there is a space between Fri, and 30 -> "Fri, 30-Jul-21 86 70,0 36"
Yes, it cleans it up and makes each date a consistent format. It is customary to put a space after the comma, but if it is not desired it can be removed from the replacement and all results will consistently have no space between the comma and the day.
0

This is one way to do it:

var rows = new[]
{
    "Wed2,8-Jul-21 64 53,0 57",
    "Thu2, 9-Jul-21 73 60,0 48",
    "Fri, 30-Jul-21 86 70,0 36",
    "Sat,31-Jul-21 84 69,0 38",
    "Sun0, 1-Aug-21 89 73,0 33",
    "Mon0,2-Aug-21 98 80,0 24",
    "Tue0, 3-Aug-21 91 75,0 31",
    "Wed0,4-Aug-21 92 75,0 30"
};

var rg = new Regex(@"^(\w{3})\d?, ?(\d.*)");

foreach (var row in rows)
{
    var fixedRow = rg.Replace(row, "$1,$2");
    Console.WriteLine(fixedRow);
}

Working example: https://dotnetfiddle.net/uAMx06

Explanation:

The general idea is to match the parts you are interested in, store them with capture groups, and then rebuild a fixed string from those components.

^          Matches the beginning of the input.
(          Starts a capture group. Everything that is matched
             between '(' and ')' will be stored and can be
             referenced with $1.
\w{3}      Matches any word character exactly three times.
)          Closes the capture group.
\d?        Matches a digit 0 or 1 times.
,          Matches a literal ','.
 ?         Matches a space character 0 or 1 times.
(          Starts another capture group, referenced with $2.
\d         Matches a single digit.
.*         Matches any string of characters
)          Closes the last capture group.

3 Comments

Thanks for your answer. but in the first one shouldn't remove 2. the correct value is Wed,28-Jul-21 64 53,0 57
Thanks for the cool explanation. I made a small change in your pattern. ^(\w{3})(\d)?, ?(\d.*) and replacement is $1,$2$3
Just an observation that this regex pattern overmatches the error condition and "corrects" cases like Fri, 30-Jul-21, making it work harder than it has to. I'd remove the first ?.

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.