Essentially I have a string: string str = "Hello.... My name is Steve.. I like Dogs."
I need to change occurrences of "..." or more periods into just 3. Any occurrences of 2 need to become 1.
Using
Regex.Replace(str)
Regex.Replace(str,"[.]{3,}","...")
works great at changing the groups greater then 3 to 3. But I can't select groups of 2 "[.]{2}" because the groups of 3 are made up of 2...
My final String needs to look like this: string str = "Hello... My name is Steve. I like Dogs."
If you are allowed to use c# features:
string input = "Hello.... My name is Steve.. I like Dogs.";
string output = Regex.Replace(input, @"\.{2,}", m => m.Length == 2 ? "." : "...");
正则表达式具有负的前瞻性和后瞻性:
(?<!\.)\.\.(?!\.)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.