I'm new to regular expressions and java so please bear with my newbish question.
I want to do the following:
If I have a string:
"I like ice cream only if it is chocolate ice cream. Chocolate cream"
and a pattern like
"chocolate ice cream"
I want to match and replace all words matched with a # surrounding them. Like this:
"I like #ice cream# only if it is #chocolate ice cream#. #Cholcolate cream#"
I used java's regex api and I understand I can use Matcher.replaceAll. But I'm having trouble coming up with a proper regex. I came up with this chocolate*\\s*ice*\\s*cream*. But the problem here is it's only matching the whole substring, i.e "chocolate ice cream". I think something like this could work:
chocolate|ice|cream|chocolate ice|ice cream|chocolate cream|chocolate ice cream
etc, i.e all permutations, but this would be cumbersome as the substring grows.
I would appreciate any ideas on proceeding in the right direction.
ice chocolateto match?