简体   繁体   English

正则表达式可以匹配字母数字字符或连字符以外的任何内容

[英]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.

相关问题 正则表达式在括号中匹配连字符斜杠字母数字 - regex match hyphen slash alphanumeric in parentheses 正则表达式匹配字母数字字符和 & 符号 - Regex to match alphanumeric character and ampersand(&) 正则表达式只匹配字母数字和连字符,在javascript中删除其他所有内容 - regex to match alphanumeric and hyphen only, strip everything else in javascript 正则表达式仅匹配字母数字,连字符,下划线和句点,没有重复的标点符号 - regex to match only alphanumeric, hyphen, underscore, and period, with no repeating punctuation 什么是匹配字母数字6字符串的正则表达式? - What is the regex to match an alphanumeric 6 character string? 正则表达式将alpha字符与第一个字符匹配,其余为字母数字 - Regex expression to match alpha character as first character and the rest alphanumeric 正则表达式匹配除空格之外的单个字符 - regex to match a single character that is anything but a space Java 中的正则表达式 - 匹配除“*”字符以外的任何内容 - Regex in Java - Match anything, except '*' character 如何获取以连字符开头的匹配字符串和使用正则表达式的字符 - how to get match string that start with hyphen and character with regex JavaScript Regex仅适用于第一个字符字母,第二个及以后可以是字母数字或特殊字符(连字符,逗号和空格) - JavaScript Regex for first character alphabet only, second and onwards can be alphanumeric or special characters (hyphen, comma and whitespace)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM