Reputation: 755
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)){
.....
}
Upvotes: 0
Views: 120
Reputation: 5452
Try
a[^\w]?word
This will match any a
followed by anything but a char, followed by word
.
To match a narrower range of strings use [-\s]
Put (?i)
at the beginning of the regex to make it case insensitive
(?i)a[^\w]?word
(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
((a word)|(a-word)|(aword)|(AWord)|(A-WORD))
because it will match exactly what you need (if you know the exact domain of what you are looking for)
Upvotes: 2
Reputation: 1561
Try this : (?:a[ -]?word|A-?W(?:ord|ORD))
It will match all words you listed
Upvotes: 0