求教几个正则

微醺_zZ 2009-07-02 04:19:04
验证decimal(4,2),decimal(5,2),decimal(2,1),int类型,字符串长度等。


//验证是否为dec(5,2)小数
public bool IsValidDecimal5_2(string strIn)
{

//return Regex.IsMatch(strIn, @"[0].\d{1,2}|[1]");
return Regex.IsMatch(strIn, @"[0-150].\d{0,2}}");
}

//验证是否为dec(4,2)小数
public bool IsValidDecimal4_2(string strIn)
{

//return Regex.IsMatch(strIn, @"[0].\d{1,2}|[1]");
return Regex.IsMatch(strIn, @"[0-99].\d{0,2}");
}

//验证是否为dec(4,2)小数
public bool IsValidDecimal2_1(string strIn)
{

//return Regex.IsMatch(strIn, @"[0].\d{1,2}|[1]");
return Regex.IsMatch(strIn, @"[0-9].\d{0,1}");
}

//验证是否为int类型整数
public bool IsValidInt(string strIn)
{

//return Regex.IsMatch(strIn, @"[0].\d{1,2}|[1]");
return Regex.IsMatch(strIn, @"\d");
}


就一个decimal(2,1)的好用,搞不懂了,求教~~
...全文
22 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
微醺_zZ 2009-07-03
  • 打赏
  • 举报
回复
Zhanlixin你好,谢谢你的热心回答,你写的正则之前我也这么写过,可是看似理论上正确了,却有很多的错误,可以用RegexTester来测试一下。具体我也不知道为什么...
还是得谢谢你的热心帮助
wackyboy 2009-07-03
  • 打赏
  • 举报
回复

//验证是否为dec(5,2)小数
public bool IsValidDecimal5_2(string strIn)
{
return Regex.IsMatch(strIn, @"^[+-]?(?=([0-9]{1,5}$|[0-9.]{3,6}$))(0|[1-9][0-9]{0,4})(\.[0-9]{1,2})?$",RegexOptions.ExplicitCapture);
}

//验证是否为dec(4,2)小数
public bool IsValidDecimal4_2(string strIn)
{
return Regex.IsMatch(strIn, @"^[+-]?(?=([0-9]{1,4}$|[0-9.]{3,5}$))(0|[1-9][0-9]{0,3})(\.[0-9]{1,2})?$", RegexOptions.ExplicitCapture);
}

//验证是否为dec(2,1)小数
public bool IsValidDecimal2_1(string strIn)
{
return Regex.IsMatch(strIn, @"^(?=([0-9]{1,2}$|[0-9.]{3}$))(0|[1-9][0-9]{0,1})(\.[0-9])?$", RegexOptions.ExplicitCapture);
}

//验证是否为int类型整数
public bool IsValidInt(string strIn)
{
return Regex.IsMatch(strIn, @"^[+-]?(0|[1-9][0-9]*)$", RegexOptions.ExplicitCapture);
}
Zhanlixin 2009-07-03
  • 打赏
  • 举报
回复
//验证是否为dec(5,2)小数
public bool IsValidDecimal5_2(string strIn)
{
return Regex.IsMatch(strIn, @"\d{1,3}\.\d{0,2}");
}

//验证是否为dec(4,2)小数
public bool IsValidDecimal4_2(string strIn)
{
return Regex.IsMatch(strIn, @"\d{1,2}\.\d{0,2}");
}

//验证是否为int类型整数
public bool IsValidInt(string strIn)
{
return Regex.IsMatch(strIn, @"\d+");
}
微醺_zZ 2009-07-03
  • 打赏
  • 举报
回复
楼上的,先谢谢你的热心回答,但是你写出的正则根本就不对,比如dec(5,2),数字的位数最多是5位,其中整数部分1~3位,小数部分0~2位,你的正则连最标准的123.45都校验不过,继续求教
Zhanlixin 2009-07-02
  • 打赏
  • 举报
回复
//验证是否为dec(5,2)小数
public bool IsValidDecimal5_2(string strIn)
{
return Regex.IsMatch(strIn, @"\d{5}\.\d{2}");
}

//验证是否为dec(4,2)小数
public bool IsValidDecimal4_2(string strIn)
{
return Regex.IsMatch(strIn, @"\d{4}\.\d{2}");
}

//验证是否为int类型整数
public bool IsValidInt(string strIn)
{
return Regex.IsMatch(strIn, @"\d+");
}
微醺_zZ 2009-07-02
  • 打赏
  • 举报
回复
没人答啊,就几个正则表达式而已...小弟急啊,快帮忙看看

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧