I want to understand the following regex:
String.replaceAll("(.)(?=.*\\1)", "")
I understand the first part (.) is used to group a single character (anything). This is to create a back reference for \\1.
The other part (?=.*\\1)I am confused on. It says previous character 1 or 0 times followed by (not sure what = is), followed by any character 1 or more times, followed by a back reference.
If I input the following hello12hel it removes the duplicates. Can you explain how it is matching the duplicate?
Another question I had is that why a grouping is required on (?=.*\\1). Regex fails when it is not provided (i.e. if I do String.replaceAll((.)?=.*\\1) ). (Also why can't we use $1 instead of \\1?)