正则表达式匹配所有字母数字和某些特殊字符? [英] Regex to match all alphanumeric and certain special characters?

查看:2149
本文介绍了正则表达式匹配所有字母数字和某些特殊字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得一个正则表达式的工作,将允许所有字母数字字符(包括瓶盖和非帽,以及数字),但也允许空格,正斜杠(/),破折号( - )及加号(+)?

I am trying to get a regex to work that will allow all alphanumeric characters (both caps and non caps as well as numbers) but also allow spaces, forward slash (/), dash (-) and plus (+)?

我已经玩了refiddle:的http:// refiddle。 COM / GQR 但至今没有成功,任何人任何想法?

I have been playing with a refiddle: http://refiddle.com/gqr but so far no success, anyone any ideas?

我不知道这有什么差别,但我试图做到这一点C#?

I'm not sure if it makes any difference but I am trying to do this in c#?

推荐答案

如果你想允许的只有的这些,您还需要使用的锚 ^ $

If you want to allow only those, you will also need the use of the anchors ^ and $.

^[a-zA-Z0-9_\s\+\-\/]+$
^                    ^^

这是你的正则表达式,我添加的字符从第二行显示。不要忘了 + * 接近尾声,以允许在超过1个字符(0或更多的情况下, * ),否则正则表达式将尝试匹配只有一个字符,甚至与 .Matches

This is your regex and I added characters as indicated from the second line. Don't forget the + or * near the end to allow for more than 1 character (0 or more in the case of *), otherwise the regex will try to match only one character, even with .Matches.

您也可以替换全班 [A-ZA-Z0-9 _] 一个 \\ \\w ,就像这样:

You can also replace the whole class [A-Za-z0-9_] by one \w, like so:

^[\w\s\+\-\/]+$

编辑:

您可以真正避免一些逃逸,并避免最后一个精心布置逃逸(即保证了 - 为开头或结尾):

You can actually avoid some escaping and avoid one last escaping with a careful placement (i.e. ensure the - is either at the beginning or at the end):

^[\w\s+/-]+$

这篇关于正则表达式匹配所有字母数字和某些特殊字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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