Regex:
(.)(?=.*\1)
Analysis:
(.) 獲取一個(gè)字符
(?=.*\1) 零寬斷言,向前正向嘗試查找之前出現(xiàn)的字符
最后替換所有匹配到的字符為空
Code:
(java)
import java.util.regex.Pattern;
...
String regex = "(.)(?=.*\1)";
String strToMatch = "123123123";
String result = Pattern.compile(regex).matcher(strToMatch).replaceAll("")