简体   繁体   English

在 Java 正则表达式中,如何获取字符类(例如 [az])以匹配 - 减号?

[英]In a java regex, how can I get a character class e.g. [a-z] to match a - minus sign?

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 "-" ?是否可以让字符类匹配“-”?

不要在字符之间放置减号。

"[a-z-]"

转义减号

[a-z\\-]

Inside a character class [...] a - is treated specially(as a range operator) if it's surrounded by characters on both sides.在字符类[...] a -两边都被字符包围,它会被特殊对待(作为范围运算符)。 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.将匹配字符串开头的小写字母和结尾的一个或多个-

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]”匹配小写字符、数字或破折号。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用正则表达式在望头中给出范围,例如^(?=(。* [az]){1,3})(?=。* [0-9])。{2,5} $ - How to give range in lookhead using regex e.g ^(?=(.*[a-z]){1,3})(?=.*[0-9]).{2,5}$ 如何在此正则表达式中包含减号? - How can I include a minus sign in this regex? Java Regex az,AZ,0-9和(。)(_)( - ) - Java Regex a-z, A-Z, 0-9 and (.)(_)(-) 如何从另一个类访问对象(例如ArrayList)? - How can I access an object (e.g. ArrayList) from another class? Java:如何获得一个月中x天的日期(例如2012年2月的第三个星期一) - Java: How do I get the date of x day in a month ( e.g. Third Monday in February 2012) 如何使用eclipse中编写的Java代码在远程桌面中导航(例如,打开Mozilla)? - How can i navigate (e.g. open Mozilla) in a Remote Desktop by using java code written in eclipse? 我如何从Locale.getISOCountries()获得“ en_US”; - How can I get e.g. “en_US” from Locale.getISOCountries(); 如何制作一个正则表达式来匹配以 0-9 或 az 开头的带有重音符号的字符串,并且必须只接受这个特殊字符 - 单词之间的 _ '? - How to make a regex to match string that starts with 0-9 or a-z with accents and must accept only this special character - _ ' between words? Java Regex:如何实现:[AZ] [az] *后跟(((“”(空白)或“-”),然后再次[AZ] [az] *)(x次)或此字符串的结尾 - Java Regex: How to realize: [A-Z][a-z]* followed either by ((“ ” (blank) or “-”) and [A-Z][a-z]* again) (x-times) or the end of this string 如何从给定的字符串中获取字符数组(Az,无数字)? - How to get an array of character (A-z, no numbers) from a given string?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM