当前位置: 代码迷 >> .NET Framework >> 求正则表达式 如:1,3-5,10解决方案
  详细解决方案

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

热度:118   发布时间:2016-05-02 00:58:48.0
求正则表达式 如: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);            }        }    }}
  相关解决方案