正则表达式 - 没有特殊字符 [英] Regex - Without Special Characters

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

问题描述

我正在使用正则表达式来验证用户名

I'm using regex to validate username

^[a-zA-Z]+\.[a-zA-Z]{4,10}^'

不幸的是,如果该值包含特殊字符,例如 !@#$%^&*)(':;

Unfortunately it doesn't affect if the the value contains special characters such as !@#$%^&*)(':;

我很乐意为包含以下内容的 Regex 获得一些帮助:

I would glad to get some help for Regex that contains:

  • 仅限字母数字 (a-zA-Z0-9)
  • 长度在 4 - 10 个字符之间.

推荐答案

您指定的条件不符合您发布的正则表达式.

The conditions you specified do not conform to the regexp you posted.

你发布的正则表达式 ^[a-zA-Z]+\.[a-zA-Z]{4,10}^ 我猜是错误的,因为 ^ 结尾,永远不会匹配到任何表达式,如果要匹配表达式末尾的 ^ 需要转义就像这样\^.但是 ^ 单独表示这里是表达式的开始",而 $ 表示这里是表达式的结束".

the regexp you posted ^[a-zA-Z]+\.[a-zA-Z]{4,10}^ is erroneous I guess, because of the ^ in the end, it will never be matched to any expression, if you want to match with the ^ at the end of the expression, you need to escape it like this \^. but ^ alone means "here is the start of the expression", while $ means "here is the end of the expression".

尽管如此,它表示:

  • 它以 alpha 开头(至少为 1).
  • 必须有一个."句号.
  • 现在必须至少有 4 个 alpha.

你真正需要的正则表达式是:

The regexp you need is really is:

^[a-zA-Z0-9]{4,10}$

这说:

  • 以字母数字开头.
  • 最少 4 个字母数字,最多 10 个字母数字.
  • 表达式结束.

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

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