Sys
Sys

Reputation: 413

Regex repeated replace

Is it possible to have a single but recurring regex.replace call? e.g.

string dateText = "01\.02\\.2008";
string dateSeperators = @"\.|/|\\|-";
string result = Regex.Replace(dateText, dateSeperators, "."); // needs to be fixed. single call possible?

The result should give "01.02.2008". Currrently i need 2 runs, first run the above replace then replace multiple occurence of dots.

Upvotes: 0

Views: 294

Answers (4)

Hans Kesting
Hans Kesting

Reputation: 39284

Yes, use

string dateSeparators = @"(\.|/|\\|-)+";

to catch multiple separators in one go.

See this MSDN page for details on regex quantifiers (like that "+").

Upvotes: 3

Alan Moore
Alan Moore

Reputation: 75222

string dateSeparators = @"[./\\-]+";

Upvotes: 0

mcrumley
mcrumley

Reputation: 5700

string dateSeperators = @"(\.|/|\\|-)+";

That will match all repeating seperators.

Upvotes: 0

John Weldon
John Weldon

Reputation: 40749

Try using this for your dateSeperators:

string dateSeperators = @"(\.|/|\\|-)+"

This yields:

01.02.2008

Upvotes: 0

Related Questions