I tried to do this in order to replace any duplicate letters with the lower case version of my letter (in java). For instance:
I need a function that displays:
bob -> bob bOb -> bob bOOb -> bob bOob -> bob boOb -> bob bob -> bob Bob -> Bob bOb -> bob
However, I was not able to do this with regular expressions (in Java).
I tried the following:
String regex = "([A-za-z])\\1+"; String str ="bOob"; Pattern pattern = Pattern.compile(regex , Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(str); System.out.println(matcher.replaceAll("$1"));
However, this returns bOb, not bob. (it works on boOb).
I also tried:
Pattern pattern = Pattern.compile("(?i)([A-Za-z0-9])(?=\\1)", Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(str); return matcher.replaceAll("");
This solves one problem, now bOob -> bob, but brings another problem, because now it maps boOb to bob.
NOTE: it should also display BOobOoboObOoObooOoOoOoOoOOb → Bobobobobob.
I feel that at this point it is easiest to double-check the string and make some logic based on each character, but I just did not want to refuse to use regular expressions ... If there is a solution using regular expressions, it is more likely to be more efficient than a loop passing through each character?
Thanks in advance!
PS: I know that to pass a string you could just omit everything, but this is not what I wanted because it displays:
Bob → bob
source share