读书人

求正则表达式 如:1,3-5,10解决方案

发布时间: 2012-01-05 22:36:54 作者: rapoo

求正则表达式 如:1,3-5,10
只能输入数字,逗号,横杠

[解决办法]
eg:
Regex regex = new Regex(@"^[\d,-]*$");
String str = "234,--,";
if (regex.IsMatch(str))
Console.WriteLine("ok");
else
Console.WriteLine("no");
[解决办法]

C# code
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Text.RegularExpressions;namespace ConsoleApplication1{    class Program    {        static void Main(string[] args)        {            string[] yourstr = new string[] { "1,2,3", "1,2,-3", "1-2-3","dd.dd" };            Regex re = new Regex(@"^(\d+[,-])*\d+$");            foreach (string s in yourstr)            {                Console.WriteLine("{0} {1}",s,re.Match(s).Success);            }        }    }} 

读书人网 >.NET Framework

热点推荐