I have text with multiple #{key} phrases. For example:
Lorem ipsum dolor sit amet, consectetur adipisicing #{key1}. Proin nibh
augue, suscipit a, scelerisque #{key1}, lacinia in, mi. Cras vel #{key2}.
Etiam pellentesque aliquet tellus. Phasellus pharetra nulla ac diam.
Quisque semper #{key3} at risus.
I need to replace all #{key} values with corresponding messageSource.getMessage(key, null, locale) (messageSource is org.springframework.context.MessageSource), but i’m not good at regex. How to build right regular expression ?
Examples:
#{texts.appName} need to replace with messageSource.getMessage("texts.appName", null, locale);
#{my.company} need to replace with messageSource.getMessage("my.company", null, locale);
Assuming
keyis just a placeholder for any name your regex would be something like this:#\{([\w\.]+)\}This means: any sequence of word characters or dots (
\w\., which is equivalent toa-zA-Z0-9_\.) between#{and}is returned as group 1.Now you need to create a matcher and iterate over the matches, extract the key and replace the match with your message: