I have been playing with this regular expression in Java for many years and can't get it to work:
(?:^| )(?:the|and|at|in|or|on|off|all|beside|under|over|next)(?: |$)
Following:
pattern.matcher("the cat in the hat").replaceAll(" ")
gives me cat the hat . Another input example is the cat in of the next hat , which gives me the cat of next hat .
Is there any way to do this job of replacing regular expressions without having to split them into several separate regular expressions for each word and try to replace the string again?
source share