日期:2014-05-20  浏览次数:20767 次

求正则表达式 如: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);
            }
        }
    }
}