例如字符串为“123abcd22.22qwer33.33asdf44.44zxcv”,将22.22和33.33互换位置
解决方案
10
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string s = "123abcd22.22qwer33.33asdf44.44zxcv";
int i = 0;
s = Regex.Replace(s, "\d+\.\d+", m =>
{
i++;
if (i == 1) return Regex.Matches(s, "\d+\.\d+")[1].Value;
if (i == 2) return Regex.Matches(s, "\d+\.\d+")[0].Value;
return m.Value;
});
Console.WriteLine(s);
}
}
}
15
string s = "123abcd22.22qwer33.33asdf44.44zxcv"; Console.WriteLine(Regex.Replace(s, @"(\d+\.\d+)(.+?)(\d+\.\d+)", "$3$2$1"));
15