tags:

views:

25

answers:

4

Hi

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.

+3  A: 

Yes, use

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

to catch multiple separators in one go.

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

Hans Kesting
+1 for being first.
Brian
A: 

Try using this for your dateSeperators:

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

This yields:

01.02.2008
John Weldon
A: 
string dateSeperators = @"(\.|/|\\|-)+";

That will match all repeating seperators.

mcrumley
A: 
string dateSeparators = @"[./\\-]+";
Alan Moore