正则表达式 [A-z] 和 [a-zA-Z] 之间的区别 [英] Difference between regex [A-z] and [a-zA-Z]
问题描述
我正在使用正则表达式为我只需要字母字符的文本框编写输入验证器.我想知道 [A-z]
和 [a-zA-Z]
是否等效,或者在性能方面是否存在差异.
I am using a regex to program an input validator for a text box where I only want alphabetical characters. I was wondering if [A-z]
and [a-zA-Z]
were equivalent or if there were differences performance wise.
我一直在搜索[a-zA-Z]
,但没有提到[A-z]
.
I keep reading [a-zA-Z]
on my searches and no mention of [A-z]
.
我正在使用 java 的 String.matches(regex)
.
I am using java's String.matches(regex)
.
推荐答案
[Az]
将匹配 A
到 z
范围内的 ASCII 字符code>,而 [a-zA-Z]
将匹配 A
到 Z
和 范围内的 ASCII 字符> 范围从 a
到 z
.乍一看,这似乎是等效的——但是,如果您查看此表 ASCII 字符,您会将看到 Az
包含几个其他字符.具体来说,它们是[
、、
]
、^
、_
和`
(您显然不想要).
[A-z]
will match ASCII characters in the range from A
to z
, while [a-zA-Z]
will match ASCII characters in the range from A
to Z
and in the range from a
to z
. At first glance, this might seem equivalent -- however, if you look at this table of ASCII characters, you'll see that A-z
includes several other characters. Specifically, they are [
, ,
]
, ^
, _
, and `
(which you clearly don't want).
这篇关于正则表达式 [A-z] 和 [a-zA-Z] 之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!