将数字按照3位一组添加逗号的正则替换

这段正则使用了前瞻的方法,匹配的数字后面必须有3的n倍个数字,保证3个为1组,并将匹配出来的数字后面添加逗号。(正负数通杀)

 1 using System;

 2 using System.Collections.Generic;

 3 using System.Linq;

 4 using System.Text;

 5 using System.Text.RegularExpressions;

 6 using System.Threading.Tasks;

 7 

 8 namespace MyRegexTest

 9 {

10     class Program

11     {

12         static void Main(string[] args)

13         {

14             List<string> list = new List<string>() { "1", "12", "123", "1234", "12345", "123456", "1234567", 

15                 "12345678", "123456789", "1234567890"};

16             foreach (var s in list)

17             {

18                 Console.WriteLine(Regex.Replace(s, @"(\d+?)(?=(?:\d{3})+$)", "$1,"));

19             }

20 

21             Console.ReadKey();

22         }

23     }

24 }
View Code

 refer to:http://www.cnblogs.com/fysn/archive/2012/02/24/2367024.html

你可能感兴趣的:(正则)