开发者

Easy RegEx expression for someone who knows regex

I know very little about regex so this has proven very challenging for me.

I have a form that I need to make sure only contains A-Z or a-z or 1-0 and a few other "approved" characters and the ' ' (space). When spelled out, I only want to allow for:

`abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 /:;,.-()`  

From another stackoverflow question, I did find this regex: ^((?:[A-Za-z0-9-'.,@:?!()$#/\\]+|&[^#])*&?)$ but I think it is allowing for too many special characters and also in the notes, someone commented about c# so I'm not sure if it is applicable in开发者_JAVA技巧 the way I'm trying to use it. I'm using coldfusion, jquery, and javascript. I'm actually using a jQuery plug-in that I'm trying to add a validation rule for (plug-in is http://bassistance.de/jquery-plugins/jquery-plugin-validation/).

An example of an INvalid entry into the form is: MC&I Enterprise but MC/I is acceptable

Can some one help?


To start practicing with regex's, you may consider downloading the regex coach: http://weitz.de/regex-coach/ it's a free program that explains regex's and allows to see how the regex parsing works.

You may start using the \w shorthand: it matches any so-called "word character", that is A-Z, a-z, 0-9 and _ (but it does not include any kind of whitespace); then you can add all the characters you need to the character class.

If "_" (underscore) is in the list of the allowed characters, you could use this regex (that allows an empty string):

^[\w ()/:;,.-]*$

Or, if you want to validate only non-empty strings, you could use this one (which has the + sign instead of the *, meaning respectively "match one or more characters" and "match zero or more characters"):

^[\w ()/:;,.-]+$

If "_" (underscore) is not in the list of the allowed characters, then you have to use the intervals explicitly:

^[a-zA-Z\d ()/:;,.-]*$

or

^[a-zA-Z\d ()/:;,.-]+$


The following should do:

'^[a-zA-Z0-9\ /:;,\.\-\(\)]*$'

I don't think the colon, comma, or semicolon need to be escaped, but if it gives you trouble, add a backslash in front of those, too. Some of the other escapes may be overkill, but shouldn't hurt anything.


You can basically convert you list of approved characters into a character class like so:

^[a-zA-Z0-9 /:;,.\-()]+$

That should only match if the full text contains only the characters on your allowed list (more complicated rules, e.g. no spaces on the end, etc. might require a more regular expression.

This site is an extremely useful introduction to regular expressions.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜