我需要这件事的帮助。查看以下正则表达式:
Pattern pattern = Pattern.compile("[A-Za-z]+(\\-[A-Za-z]+)"); Matcher matcher = pattern.matcher(s1);
我想查找这样的词:“自制”,“ aaaa-bbb”而不是“ aaa-bbb”,而 不是 “ aaa–aa–aaa”。基本上,我想要以下内容:
单词-连字符-单词。
它适用于所有内容,但该模式会通过:“ aaa–aaa–aaa”,但不应通过。哪种正则表达式适用于此模式?
可以从表达式中删除反斜杠:
"[A-Za-z]+-[A-Za-z]+"
下面的代码应该工作
Pattern pattern = Pattern.compile("[A-Za-z]+-[A-Za-z]+"); Matcher matcher = pattern.matcher("aaa-bbb"); match = matcher.matches();
请注意,您可以使用Matcher.matches()代替Matcher.find()来检查匹配的完整字符串。
Matcher.matches()
Matcher.find()
相反,如果您想使用查找字符串Matcher.find(),则可以使用表达式
"(^|\\s)[A-Za-z]+-[A-Za-z]+(\\s|$)"
但是请注意,那么只会找到由空格分隔的单词(即,没有类似的单词aaa-bbb.)。要捕获这种情况,还可以使用lookbehinds和lookaheads:
aaa-bbb.
"(?<![A-Za-z-])[A-Za-z]+-[A-Za-z]+(?![A-Za-z-])"
这将读取
(?<![A-Za-z-]) // before the match there must not be and A-Z or - [A-Za-z]+ // the match itself consists of one or more A-Z - // followed by a - [A-Za-z]+ // followed by one or more A-Z (?![A-Za-z-]) // but afterwards not by any A-Z or -
一个例子:
Pattern pattern = Pattern.compile("(?<![A-Za-z-])[A-Za-z]+-[A-Za-z]+(?![A-Za-z-])"); Matcher matcher = pattern.matcher("It is home-made."); if (matcher.find()) { System.out.println(matcher.group()); // => home-made }