不允许使用多个反斜杠和连字符的正则表达式 [英] Regular expression for more than one back slash and hyphen not allowed

查看:242
本文介绍了不允许使用多个反斜杠和连字符的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我在C#应用程序中工作,我想对文本框进行验证,它不应该允许我多个反斜杠和多个连字符。那么有谁能告诉我如何使用正则表达式验证?



我将非常感谢你的帮助。



谢谢,

Hetal Gadhavi

解决方案

单个正则表达式并不容易 - 这不是他们的意思因为你必须自己允许所有可能的排列。



如果你在C#中这样做是为了你说,然后我会用两个简单的正则表达式:

正则表达式连字符= 正则表达式(   - ); 
正则表达式反斜杠= 正则表达式( @ \\);
string inp = @ 123-22 -2\3\" ;
if (hyphens.Matches(inp).Count > 1 || backslashes.Matches(inp).Count > 1
{
// 失败
}


Hi,

I am working in C# Application, I want to do validation of textbox that It should not allow me more than one back slash and more than one hyphen. So can anyone tell me how to do using Regular expression validation?

I''ll be grateful for your help.

Thanks,
Hetal Gadhavi

解决方案

That isn''t easy in a single regex - that isnt what they were created for, and it is a bit of a pain to implement, as you have to allow for all possible permutations yourself.

If you are doing this in C# as you say, then I would do it with two simple regexes:

Regex hyphens = new Regex("-");
Regex backslashes = new Regex(@"\\");
string inp = @"123-22-2\3";
if (hyphens.Matches(inp).Count > 1 || backslashes.Matches(inp).Count > 1)
    {
    // Failed
    }


这篇关于不允许使用多个反斜杠和连字符的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆