openshac
openshac

Reputation: 5165

Cleaning badly formed Dates - using RegEx?

I am parsing some date from a 3rd party source, unfortunately some are badly formed. What would be the best way of cleaning/sanitising them?

E.g.

Wednesday 20July -> Wednesday 20July

Tuesday20 July -> Tuesday 20 July

once I've got these I'm just converting them using

DateTime myDateTime = DateTime.Parse("Wednesday 20 July");

What would be the best way to convert these malformed dates?

I did have a play with RegEx, but I'm no expert

Regex regEx = new Regex("[0-9][a-zA-Z]");
Match match = Regex.Match("Wednesday 20July", "[0-9][a-zA-Z]");

I wasn't sure how to insert a space in the correct place once I had found a match.

Upvotes: 0

Views: 91

Answers (1)

Tim Pietzcker
Tim Pietzcker

Reputation: 336218

You could insert a space between a digit and a letter (or between a letter and a digit) like this:

resultString = Regex.Replace(subjectString, @"(?<=\d)(?=\p{L})|(?<=\p{L})(?=\d)", " ");

(?<=\d) checks if the previous character is a digit.

(?=\p{L}) checks if the following character is a letter.

Upvotes: 2

Related Questions