You can use the following pattern:
String padrao = "(\w)(\s+)(e|do|da|do|das|de|di|du)(\s+)(\w)";
This pattern has been divided into five groups, these follow the order:
+ + one or more spaces + any letter or number
Note: 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 replaceAll
, the default is found in Daniel[a de A]ndrade
, and is replaced by groups 1 and 5, which are separated by white space, these groups are represented by a > from Daniel to and A , A ndrade.
Review
To ignore uppercase and lowercase letters, you can use (?i)
in your expression, for example:
String padrao = "(?i)(\w)(\s+)(e|do|da|do|das|de|di|du)(\s+)(\w)";
The way to do the substitution is the same one as above.