[英]Regex to match anything but an alphanumeric character or a hyphen
I am trying to allow users to register a username that contains only alphabet letters (of any language), numbers, or hyphens in it. 我试图允许用户注册仅包含字母(任何语言),数字或连字符的用户名。 I'm trying to check if a username breaks this rule.
我正在尝试检查用户名是否违反了此规则。
So far this is working to find out if a username does not contain only alphanumeric characters: 到目前为止,这是工作,以找出是否一个用户名不包含字母数字字符:
REFindNoCase('^[[:alnum:]]', ARGUMENTS.Username)
Which is fine because if I get back a found result then I know its an invalid username format with special characters in it. 很好,因为如果我返回找到的结果,则知道其无效的用户名格式,其中包含特殊字符。 But I also want to allow hyphens through.
但我也想允许连字符通过。 How could I express in regex like (pseudo-code follows):
我如何用正则表达式表示(如下伪代码):
REFindNoCase('^[[:alnum:]\-]', ARGUMENTS.Username)
I can only use Perl compatible Regex because I am using ColdFusion which uses that standard mostly. 我只能使用与Perl兼容的Regex,因为我正在使用主要使用该标准的ColdFusion。
First of all, you're wrong about REFindNoCase('^[[:alnum:]]', ARGUMENTS.Username)
being fine. 首先,您认为
REFindNoCase('^[[:alnum:]]', ARGUMENTS.Username)
很好是错误的。 It checks if the first character is alphnumeric. 它检查第一个字符是否为字母数字。
$ for q in Abcdef Abc123 Abc-123 Abc/123 ; do
if echo "$q" | grep -qP '^[[:alnum:]]'
then echo "$q: match"
else echo "$q: no match"
fi
done
Abcdef: match
Abc123: match
Abc-123: match
Abc/123: match
( grep -P
uses PCRE too.) (
grep -P
使用PCRE。)
To look for character that is not an alnum
character, you'd use 要查找不是
alnum
字符的字符,可以使用
[^[:alnum:]]
As seen here: 如此处所示:
$ for q in Abcdef Abc123 Abc-123 Abc/123 ; do
if echo "$q" | grep -qP '[^[:alnum:]]'
then echo "$q: match"
else echo "$q: no match"
fi
done
Abcdef: no match
Abc123: no match
Abc-123: match
Abc/123: match
To look for character that are neither an alnum
character nor "-", you'd use 要查找的字符既不是一个
alnum
字符,也不是“ - ”,你会使用
[^[:alnum:]-]
As seen here: 如此处所示:
$ for q in Abcdef Abc123 Abc-123 Abc/123 ; do
if echo "$q" | grep -qP '[^[:alnum:]-]'
then echo "$q: match"
else echo "$q: no match"
fi
done
Abcdef: no match
Abc123: no match
Abc-123: no match
Abc/123: match
By the way, REFind
would work just as a well as REFindNoCase
since alnum
includes both uppercase and lowercase letters, so might as well use REFind
. 顺便说一句,
REFind
将工作就像以及REFindNoCase
因为alnum
包括大写和小写字母,那么不妨使用REFind
。
Update: I looked at this question RegEx: \\w - "_" + "-" in UTF-8 更新:我看着这个问题RegEx:UTF-8中的\\ w-“ _” +“-”
Final solution after trial and error (the negation had to be inside the opening bracket): 经过反复试验的最终解决方案(否定必须在左括号内):
REFind('[^[:alnum:]-]', ARGUMENTS.Username)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.