开发者

Regex repeated replace

开发者 https://www.devze.com 2023-01-07 09:52 出处:网络
Is it possible to have a single but recurring regex.replace call? e.g. string dateText = \"01\\.02\\\\.2008\";

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 s开发者_如何学编程hould give "01.02.2008". Currrently i need 2 runs, first run the above replace then replace multiple occurence of dots.


Yes, use

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

to catch multiple separators in one go.

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


Try using this for your dateSeperators:

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

This yields:

01.02.2008


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

That will match all repeating seperators.


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号