简单正则表达式需要 [英] Simple Regular Expression need

查看:63
本文介绍了简单正则表达式需要的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Vidation控件


第一个:简单的exluce某些特殊字符:

在字符串中说没有a或b或c:

[^ abc] *


第二名:

我要求的日期输入MM / DD / YYYY格式:

[0-1] [0-9] / [0-3] [0-9] / [0-9] +4如何?

I have Vidation Controls

First One: Simple exluce certain special characters:
say no a or b or c in the string:
[^abc]*

Second One:
I required date be entered in "MM/DD/YYYY" format:
[0-1][0-9]/[0-3][0-9]/[0-9]+4 How ??

推荐答案



只是不使用验证控件


它将是简单


并且使用正则表达式

验证日期没有多大意义 - 你必须解析

it在某种程度上 - 为什么不让日期解析器完成它的工作?


然后你也可以自己做日期健全检查


(另一个asp.net控件的例子使一个简单的任务复杂化)


just don''t use the validation controls

and it will be easy

and there isn''t much point in using regex
to validate dates - you will have to parse
it at some point anyway - why not let
the date parser do its job?

and then you can do your own date sanity check as well

(yet another example of asp.net controls complicating a simple task :)


Q:

看看 http://www.regexlib.com/ ,你应该能够找到你想要的那些




HTH


河流先生:

至于不使用验证控件,为什么不让他们完成工作呢?

你争论(实际上,那个最的你似乎在这里做什么),其"还有

在使用正则表达式验证日期应该够远点"以下是使用正则表达式验证日期的一些好的原因:


1.)DateTime.Parse()在客户端上不可用。你有抱怨
抱怨Visual Studio / ASP.NET不允许你内联的方式

ASPX中的代码块中的HTML和JavaScript所以它可以是暗示

你至少在一些应用程序中使用JavaScript。当然,一个用户

可能会禁用JavaScript,但为什么要惩罚那些不会因为强制回发而直接告诉 $ b的用户br />
用户在没有回发的情况下,当几行JavaScript可以执行相同的操作时,他们的输入无效。对于拨号连接上的用户来说回拨可能特别麻烦,因为往返行程

将比执行几行JavaScript花费更长的时间。

是否应在服务器上仔细检查输入?当然,但是,b $ b并不意味着不应该提前检查数据,以减少服务器的工作量。 [javascript函数Date.parse()是另一种选择但是很奇怪,并且需要使用两个单独的

验证例程(客户端和服务器)而不是让它们验证

控制完成它的工作]。


2.)当输入的格式不正确时,DateTime.Parse()会抛出一个

异常,必须处理(昂贵)并最终膨胀你的

代码(你似乎不太喜欢基于你的一些

以前的帖子)与try / catch块。相反,可以选择使用

Regex.IsMatch()返回一个布尔值,可以在调用DateTime.Parse()之前对其进行测试,并避免与之相关的开销。例外

处理。


----------------

Dave Fancher
http://www.davefancher.com


" John Rivers" <音响***** @ btinternet.com>在消息中写道

news:11 ********************** @ g49g2000cwa.googlegr oups.com ...
Q:
Take a look at http://www.regexlib.com/, you should be able to find what
you''re looking for there.

HTH

Mr. Rivers:
As for not using the validation controls, why not let them do their job?
You argue (actually, that''s most of what you seem to do here) that "there
isn''t much point in using regex to validate dates." Here are some good
reasons to use regular expressions to validate a date:

1.) DateTime.Parse() is not available on the client. You have
complained about the way that Visual Studio/ASP.NET don''t let you inline
HTML and JavaScript in a code block in the ASPX so it can be implied that
you use JavaScript in at least some of your applications. Granted, a user
may have JavaScript disabled but why should you punish those users that
don''t by forcing a postback that ends up doing nothing more than telling the
user that their input is invalid when a couple of lines of JavaScript could
do the same thing without the postback. The postback can be particularly
troublesome for those users on a dial-up connection since the round trip
will take significantly longer than executing a couple lines of JavaScript.
Should the input be double checked on the server? Absolutely, but that
doesn''t mean that the data should not be checked ahead of time to reduce
your server''s workload a bit too. [The javascript function Date.parse() is
an alternative but is quirky at best and would require using two separate
validation routines (client and server) rather than letting the validation
control do its job].

2.) When the input is in an incorrect format, DateTime.Parse() throws an
exception which must then be handled (expensive) and ends up bloating your
code (something you seem to not be too fond of based on some of your
previous posts) with try/catch blocks. Instead, one may choose to use
Regex.IsMatch() which returns a boolean that can be tested against before
calling DateTime.Parse() and avoids the overhead associated with exception
handling.

----------------
Dave Fancher
http://www.davefancher.com

"John Rivers" <fi*****@btinternet.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...

只是没有使用验证控件

这将很容易

并且使用正则表达式并没有多大意义br />来验证日期 - 无论如何你必须在某个时候解析它 - 为什么不让日期解析器完成它的工作?

然后你可以做你的自己的日期健全性检查

(另一个asp.net控件的例子使一个简单的任务变得复杂:)

just don''t use the validation controls

and it will be easy

and there isn''t much point in using regex
to validate dates - you will have to parse
it at some point anyway - why not let
the date parser do its job?

and then you can do your own date sanity check as well

(yet another example of asp.net controls complicating a simple task :)



"约翰里弗斯<音响***** @ btinternet.com>写在

新闻:11 ********************** @ g49g2000cwa.googlegr psps.com:
"John Rivers" <fi*****@btinternet.com> wrote in
news:11**********************@g49g2000cwa.googlegr oups.com:
并且使用正则表达式验证日期并没有多大意义 - 无论如何你必须在某个时候解析
- 为什么不让日期解析器呢?它的工作?
and there isn''t much point in using regex
to validate dates - you will have to parse
it at some point anyway - why not let
the date parser do its job?



因为正则表达式是一种有效的验证方式 - 在服务器和

客户端上。


和正则表达式可以做解析同样 - 你可以在

正则表达式中进行组匹配,所以不需要真正解析。


-

Lucas Tam(RE********@rogers.com)

请删除删除来自回复时的电子邮件地址。
http://成员。 ebay.com/aboutme/coolspot18/


这篇关于简单正则表达式需要的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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