我把这个交换列表中数字的linq查询放在一起。我想知道是否有使用正则表达式的更优雅的方法。
var str = " <gml:coordinates>36.230968,21.971054 36.633873,19.144154 38.007656,19.423254 37.606049,22.303988 36.230968,21.971054</gml:coordinates>";
var test = string.Format("<gml:coordinates>{0}</gml:coordinates>", string.Join(" ", Regex.Match(str, "<gml:coordinates>(.*)</gml:coordinates>").Groups[1].Value.Split(' ').Select(d => string.Join(",", d.Split(',').Reverse()))));
//<gml:coordinates>21.971054,36.230968 19.144154,36.633873 19.423254,38.007656 22.303988,37.606049 21.971054,36.230968</gml:coordinates>
以下方法应该有效:
Regex.Replace(str, @"([d.]*),([d.]*)", "$2,$1")