C# 正则怎么将字符串中前两串带小数点的数字互换位置

.Net技术 码拜 8年前 (2016-05-19) 1093次浏览
例如字符串为“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

该回复于2016-08-02 22:15:22被版主删除

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明C# 正则怎么将字符串中前两串带小数点的数字互换位置
喜欢 (0)
[1034331897@qq.com]
分享 (0)