首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET Framework >

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

2012-01-05 
求正则表达式 如:1,3-5,10只能输入数字,逗号,横杠[解决办法]eg:Regex regex new Regex(@^[\d,-]*$)St

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

热点排行