正则表达式,不允许波浪号/控制台键 [英] RegEx that doesnt allow tilde / console key
问题描述
我有以下正则表达式
(^[a-zA-z]+([a-zA-Z\s-]*)[a-zA-z]+$)
它禁止除(`)控制台键之外的所有特殊字符.有人可以解释我在正则表达式中做错什么或如何删除它吗?
It disallow all special chars except ( ` ) the console key. Could someone explain what im doing wrong in my regex or how i can remove that?
谢谢.
推荐答案
请勿使用 [Az]
,请使用 [a-zA-Z]
:
^[a-zA-Z]+([a-zA-Z\s-]*)[a-zA-Z]+$
否则, [A-z]
类也将与其他一些非字母符号匹配:
Otherwise, the [A-z]
class will match some other non-letter symbols, too:
这是regex101.com上的演示.
Here is a demo on regex101.com.
仅需注意: [A-z]
有时可用于在为特定语言设置排序规则时匹配POSIX样式正则表达式中的所有字母.
Just a note: [A-z]
can sometimes be used to match all letters in POSIX-style regex when collation is set for a particular language.
[[" ABCEDEF [] _ abcdef"=〜([A-z] +)]]&&在Cygwin上用
会产生 LC_COLLATE ="en_US.UTF-8"
在Cygwin上回显"$ {BASH_REMATCH [1]}" ABCEDF
.如果将 LC_COLLATE
设置为 C
(在Cygwin上,通过 export
完成),它将给出预期的 ABCEDEF [] _ abcdef 代码>.
[[ "ABCEDEF[]_abcdef" =~ ([A-z]+) ]] && echo "${BASH_REMATCH[1]}"
on Cygwin with LC_COLLATE="en_US.UTF-8"
yields ABCEDF
.
If you set LC_COLLATE
to C
(on Cygwin, done with export
), it will give the expected ABCEDEF[]_abcdef
.
这篇关于正则表达式,不允许波浪号/控制台键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!