Saeid Mirzaei
Saeid Mirzaei

Reputation: 1236

Format string with regex and replace value

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

Upvotes: 0

Views: 241

Answers (2)

Chris Maurer
Chris Maurer

Reputation: 2539

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

Upvotes: 2

Good Night Nerd Pride
Good Night Nerd Pride

Reputation: 8442

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.

Upvotes: 0

Related Questions