在java正则表达式中,我如何获得一个字符类,例如[a-z]匹配 - 减号?

时间:2021-07-20 06:13:50
Pattern pattern = Pattern.compile("^[a-z]+$");
String string = "abc-def";
assertTrue( pattern.matcher(string).matches() ); // obviously fails

Is it possible to have the character class match a "-" ?

字符类是否可以匹配“ - ”?

5 个解决方案

#1


35  

Don't put the minus sign between characters.

不要在字符之间加上减号。

"[a-z-]"

#2


7  

Escape the minus sign [a-z\-]

逃避减号[a-z \ - ]

#3


5  

Inside a character class [...] a - is treated specially(as a range operator) if it's surrounded by characters on both sides. That means if you include the - at the beginning or at the end of the character class it will be treated literally(non-special).

在字符类[...]内部 - 如果它被两边的字符包围,则被特别处理(作为范围操作符)。这意味着如果你包括 - 在字符类的开头或结尾,它将被字面上处理(非特殊)。

So you can use the regex:

所以你可以使用正则表达式:

^[a-z-]+$

or

要么

^[-a-z]+$

Since the - that we added is being treated literally there is no need to escape it. Although it's not an error if you do it.

由于 - 我们添加的是字面上的处理,所以没有必要逃避它。虽然如果你这样做并不是一个错误。

Another (less recommended) way is to not include the - in the character class:

另一种(不太推荐)的方法是在字符类中不包括 - :

^(?:[a-z]|-)+$

Note that the parenthesis are not optional in this case as | has a very low precedence, so with the parenthesis:

请注意,在这种情况下,括号不是可选的|具有非常低的优先级,因此使用括号:

^[a-z]|-+$

Will match a lowercase alphabet at the beginning of the string and one or more - at the end.

将匹配字符串开头的小写字母和一个或多个 - 最后。

#4


4  

I'd rephrase the "don't put it between characters" a little more concretely.

我会更具体地说“不要把它放在人物之间”。

Make the dash the first or last character in the character class. For example "[-a-z1-9]" matches lower-case characters, digits or dash.

使短划线成为角色类中的第一个或最后一个角色。例如,“[ - a-z1-9]”匹配小写字母,数字或短划线。

#5


3  

This works for me

这对我有用

   Pattern p = Pattern.compile("^[a-z\\-]+$");
   String line = "abc-def";
   Matcher matcher = p.matcher(line);
   System.out.println(matcher.matches());  // true

#1


35  

Don't put the minus sign between characters.

不要在字符之间加上减号。

"[a-z-]"

#2


7  

Escape the minus sign [a-z\-]

逃避减号[a-z \ - ]

#3


5  

Inside a character class [...] a - is treated specially(as a range operator) if it's surrounded by characters on both sides. That means if you include the - at the beginning or at the end of the character class it will be treated literally(non-special).

在字符类[...]内部 - 如果它被两边的字符包围,则被特别处理(作为范围操作符)。这意味着如果你包括 - 在字符类的开头或结尾,它将被字面上处理(非特殊)。

So you can use the regex:

所以你可以使用正则表达式:

^[a-z-]+$

or

要么

^[-a-z]+$

Since the - that we added is being treated literally there is no need to escape it. Although it's not an error if you do it.

由于 - 我们添加的是字面上的处理,所以没有必要逃避它。虽然如果你这样做并不是一个错误。

Another (less recommended) way is to not include the - in the character class:

另一种(不太推荐)的方法是在字符类中不包括 - :

^(?:[a-z]|-)+$

Note that the parenthesis are not optional in this case as | has a very low precedence, so with the parenthesis:

请注意,在这种情况下,括号不是可选的|具有非常低的优先级,因此使用括号:

^[a-z]|-+$

Will match a lowercase alphabet at the beginning of the string and one or more - at the end.

将匹配字符串开头的小写字母和一个或多个 - 最后。

#4


4  

I'd rephrase the "don't put it between characters" a little more concretely.

我会更具体地说“不要把它放在人物之间”。

Make the dash the first or last character in the character class. For example "[-a-z1-9]" matches lower-case characters, digits or dash.

使短划线成为角色类中的第一个或最后一个角色。例如,“[ - a-z1-9]”匹配小写字母,数字或短划线。

#5


3  

This works for me

这对我有用

   Pattern p = Pattern.compile("^[a-z\\-]+$");
   String line = "abc-def";
   Matcher matcher = p.matcher(line);
   System.out.println(matcher.matches());  // true