日期:2014-05-18  浏览次数:20465 次

正则表达式的完全匹配怎么写啊?
例如我判断一个字符串是不是全是数字组成的
JAVA很好搞的。C#的我真的很糊涂了。
C# code
string str = "中12";
            if (Regex.IsMatch(str,"\\d+"))
            {
                Label1.Text = "Yes";
            }
            else {
                Label1.Text = "No";
            }

这个出来居然是YES。我头大了啊

------解决方案--------------------
应该是这样的:
C# code

string str = "中12";
            if (Regex.IsMatch(str,"^\\d+$"))
            {
                Label1.Text = "Yes";
            }
            else {
                Label1.Text = "No";
            }

------解决方案--------------------
string str = "中12";
if (Regex.IsMatch(str,@"^\d+$"))
{
Label1.Text = "Yes";
}
else
{
Label1.Text = "No";
}

建议有时间去看一下正则的参考资料

Windows 脚本技术

迷你小工具V1.0(正则测试+编码转换+IP转换)