I need to make a program that searches a particular word in a set of texts and labels the searched word in the middle of the text.
For this I developed the following method:
public void grifarTexto(Relato relato, String texto) {
relato.setDescricaoRelato(relato.getDescricaoRelato().replaceAll("(?i)("
+ texto + ")", "<mark>$1</mark>"));
}
But there were two problems ...
I would like it to take the whole word, but when you put the start (^) and end ($) marking characters, it ends up not highlighting any part of the text.
Method used:
public void grifarTexto(Relato relato, String texto) {
relato.setDescricaoRelato(relato.getDescricaoRelato().replaceAll("(?i)
^(" + texto + ")$", "<mark>$1</mark>"));
}
2º He is ignoring the lowercase and lowercase characters of the word correctly, except when it has an accent. For example: When I search for the word hand
MÃO (não grifa)
mão (grifa)
mÃo (não grifa)
MãO (grifa)
In other words, it does not ignore the minuscule and minuscule characters of accented letters.
I tested these expressions on the Rubular site to see if they were correct and the return of the site appears to be ok. Links to the tests: link and link
Does anyone know which regular expression I should use to get the validations I want?