You can use the following pattern:
String padrao = "(\\w)(\\s+)(e|do|da|do|das|de|di|du)(\\s+)(\\w)";
This pattern was divided into five groups, these follow the order:
any letter or number + one or more spaces + connector + one or more spaces + any letter or number
Note: the groups are formed through parentheses.
To make the replacement use:
public static void main(String[] args) {
String padrao = "(\\w)(\\s+)(e|do|da|do|das|de|di|du)(\\s+)(\\w)";
String nome = "Daniela de Andrade";
System.out.println(nome.replaceAll(padrao, "$1 $5"));
}
The result is as follows:
Daniela Andrade
When you use the replaceAll
, the pattern is found in Daniel[a de A]ndrade
, and is replaced by groups 1 and 5, which are separated by a blank space, these groups are represented by the to, of Danielto, and the To, of Tothoroughness.
Overhaul
To ignore upper and lower case letters, you can use (?i)
in its expression, for example:
String padrao = "(?i)(\\w)(\\s+)(e|do|da|do|das|de|di|du)(\\s+)(\\w)";
The way to perform the substitution is the same as indicated above.
As you saw on the [tour], if you think the main goal of this question has been achieved, you can accept an answer. You can also vote for everything on the site you find useful, not just things related to your posts.
– mateusalxd