regex [A-z]与[a-zA-Z]之间的差异

时间:2021-04-10 21:16:38

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.

我正在使用一个regex为一个文本框编写输入验证器,我只需要字母字符。我想知道[A-z]和[a-zA-Z]是否等价,或者是否存在性能差异。

I keep reading [a-zA-Z] on my searches and no mention of [A-z].

我不断地阅读关于我的搜索的[a-zA-Z],却没有提及[A-z]。

I am using java's String.matches(regex).

我正在使用java的String.matches(regex)。

6 个解决方案

#1


58  

[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]将匹配ASCII字符的范围从A到z,而[a-zA-Z]将ASCII字符匹配的范围从A到z的范围从A到z。乍一看,这似乎是等价的,然而,如果你看看这个ASCII字符表,你会发现所有包含其他字符。\,具体地说,他们是[],^,_,' ' '(你显然不希望)。

#2


10  

The a-z matchs 'a' to 'z' A-Z matchs 'A' to 'Z' A-z matches all these as well as the characters between the 'Z' and 'a' which are [ ] ^ / _ `

所有配的一个“z”a - z配' a ' ' z '所有比赛所有这些以及人物之间的“z”和“a”[]^ / _ '

Refer to http://www.asciitable.com/

指的是http://www.asciitable.com/

#3


8  

Take a look at ASCII table. You'll see that there are some characters between Z and a, so you will match more than you intented to.

看看ASCII表。你会看到Z和a之间有一些字符,所以你会匹配比你预期的更多。

#4


6  

The square brackets create a character class and the hyphen is a shorthand for adding every character between the two provided characters. i.e. [A-F] can be written [ABCDEF].

方括号创建一个字符类,连字符是在两个提供的字符之间添加每个字符的简写。即[A-F]可以写成[ABCDEF]。

The character class [A-z] will match every character between those characters, which in ASCII includes some other characters such as '[', '\' and ']'.

字符类[A-z]将匹配这些字符之间的每个字符,在ASCII中包括一些其他字符,如'[','\'和']'。

An alternative to specifying both cases would be to set the regular expression to be case-insensitive, by using the /i modifier.

指定这两种情况的另一种方法是使用/i修饰符将正则表达式设置为不区分大小写。

#5


6  

When you take a look at the ASCII table, you will see following:

当您查看ASCII表时,您将看到以下内容:

A = 65
Z = 90
a = 97
z = 122

So, [A-z] will match every char from 65 to 122. This includes these chars (91 -> 97) as well:

因此,[A-z]将匹配65到122之间的每个字符。这包括这些chars (91 - >97):

 [\]^_`

This means [A-Za-z] will match only the alphabet, without the noticed chars

这意味着[A-Za-z]将只匹配字母表,没有注意到的字符

#6


3  

Take a look at the ASCII chart (which Java characters are based on): there are quite a few punctuation characters situated between Z and a, namely these:

请看ASCII图表(Java字符的基础):在Z和a之间有相当多的标点字符,即:

[\]^ _`

#1


58  

[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]将匹配ASCII字符的范围从A到z,而[a-zA-Z]将ASCII字符匹配的范围从A到z的范围从A到z。乍一看,这似乎是等价的,然而,如果你看看这个ASCII字符表,你会发现所有包含其他字符。\,具体地说,他们是[],^,_,' ' '(你显然不希望)。

#2


10  

The a-z matchs 'a' to 'z' A-Z matchs 'A' to 'Z' A-z matches all these as well as the characters between the 'Z' and 'a' which are [ ] ^ / _ `

所有配的一个“z”a - z配' a ' ' z '所有比赛所有这些以及人物之间的“z”和“a”[]^ / _ '

Refer to http://www.asciitable.com/

指的是http://www.asciitable.com/

#3


8  

Take a look at ASCII table. You'll see that there are some characters between Z and a, so you will match more than you intented to.

看看ASCII表。你会看到Z和a之间有一些字符,所以你会匹配比你预期的更多。

#4


6  

The square brackets create a character class and the hyphen is a shorthand for adding every character between the two provided characters. i.e. [A-F] can be written [ABCDEF].

方括号创建一个字符类,连字符是在两个提供的字符之间添加每个字符的简写。即[A-F]可以写成[ABCDEF]。

The character class [A-z] will match every character between those characters, which in ASCII includes some other characters such as '[', '\' and ']'.

字符类[A-z]将匹配这些字符之间的每个字符,在ASCII中包括一些其他字符,如'[','\'和']'。

An alternative to specifying both cases would be to set the regular expression to be case-insensitive, by using the /i modifier.

指定这两种情况的另一种方法是使用/i修饰符将正则表达式设置为不区分大小写。

#5


6  

When you take a look at the ASCII table, you will see following:

当您查看ASCII表时,您将看到以下内容:

A = 65
Z = 90
a = 97
z = 122

So, [A-z] will match every char from 65 to 122. This includes these chars (91 -> 97) as well:

因此,[A-z]将匹配65到122之间的每个字符。这包括这些chars (91 - >97):

 [\]^_`

This means [A-Za-z] will match only the alphabet, without the noticed chars

这意味着[A-Za-z]将只匹配字母表,没有注意到的字符

#6


3  

Take a look at the ASCII chart (which Java characters are based on): there are quite a few punctuation characters situated between Z and a, namely these:

请看ASCII图表(Java字符的基础):在Z和a之间有相当多的标点字符,即:

[\]^ _`