如何从文本中删除多个点(.)?
例如:
例如:
123..45 = 123.45
10.20.30 = 10.2030
12.34 = 12.34
12.. = 12
123...45 = 123.45
如何做到这一点?
提前感谢。
不一定要使用正则表达式,你可以通过以下方法实现你需要的功能:
string s = "10.20.30";
int n;
if( (n=s.IndexOf('.')) != -1 )
s = string.Concat(s.Substring(0,n+1),s.Substring(n+1).Replace(".",""));
Regex.Replace("Yourstring", "[.]{2,}",".");
using System.Linq;
...
string s = "10.20.30";
while (s.Count( c => c == '.') > 1)
s = s.Remove( s.LastIndexOf('.'),1);
好的,使用这个Regex.Replace
的重载, 这里的技巧是匹配第一个.
之后的所有内容,通过使用一个向后查找组[(?<=
],然后使用MatchEvaluator
来清除Match
。
Regex.Replace(
"127.0.8", //input string
@"(?<=\.).*", //match everything after 1st "."
m => m.Value.Replace(".",string.Empty)) //return value of match without "."