Java 正则表达式(单词的三种类型)?

Java 正则表达式(单词的三种类型)?,java,regex,match,Java,Regex,Match,我不熟悉正则表达式,需要一些帮助 我有以下几句话: this is a-word a word this is aword is this AWord is this A-WORD is this 我想知道句子中是否有单词a-word或a-word或a-word或a-word 我试过这个: String sentence = "AWord is this"; String regex = "(a(\\s,'-','')word)\\i"; if (sentence.matches( regex

我不熟悉正则表达式,需要一些帮助

我有以下几句话:

this is a-word
a word this is
aword is this
AWord is this
A-WORD is this
我想知道句子中是否有单词
a-word
a-word
a-word
a-word

我试过这个:

String sentence = "AWord is this";
String regex = "(a(\\s,'-','')word)\\i";
if (sentence.matches( regex)){
  .....
}
试一试

这将匹配任何
a
,后跟除字符以外的任何字符,后跟
word

要匹配较窄范围的字符串,请使用[-\s]

(?i)
放在正则表达式的开头,使其不区分大小写

(?i)a[^\w]?word
(,在此处搜索以不区分大小写的方式搜索字符串的其他方法)

记住将
\
转义到
\

然而,“最安全”的方法是使用这个

((a word)|(a-word)|(aword)|(AWord)|(A-WORD))
因为它将完全符合您的需要(如果您知道您正在寻找的确切领域)

尝试以下:
(?:a[-]?word | a-?W(?:ord | ord))

它将匹配您列出的所有单词

也必须不区分大小写。\\n我在Java中不是这样工作的-您需要使用Pattern.compile和不区分大小写的标志int)
((a word)|(a-word)|(aword)|(AWord)|(A-WORD))