I am new to regular expressions and need some help.
I have these sentences:
this is a-word
a word this is
aword is this
AWord is this
A-WORD is this
I’d like to know if the word a word or a-word or aword or AWord or A-WORD is in the sentence.
I tried this:
String sentence = "AWord is this";
String regex = "(a(\\s,'-','')word)\\i";
if (sentence.matches( regex)){
.....
}
Try
This will match any
afollowed by anything but a char, followed byword.To match a narrower range of strings use [-\s]
Put
(?i)at the beginning of the regex to make it case insensitive(reference, search here for other ways to search strings in a case insensitive way)
and remember to escape the
\to\\The “safest” way is however to use this one
because it will match exactly what you need (if you know the exact domain of what you are looking for)