Search code examples
javaregexconditional-statementsreplaceall

String replaceALL with conditions on letters before and after the match


I am reading in Strings from a text file

Example: Is Mississippi a State where there are a lot of Systems?

I am trying to use replace all to replace all "s" & "S" with the same case "t" or "T" unless at the start of a word and unless before or after the "s" or "S" there is another "s" or "S".

Output expected: It Mississippi a State where there are a lot of Syttemt?

I have tried...

.replaceAll("[^sStT](?!\\b)S", "T").replaceAll("[^SstT](?!\\b)s", "t"); 

The output was..."t Mtstsippi a State where there are many Sttet?"


Solution

  • You can do this with two replaceAll calls. one for s -> t and one for S -> T

    You can use a look-behind (?<=regex) and look-ahead (?=regex) group to find a pattern without replacing its contents.

    The look-behind will check that a character before s is not in a list of characters [^<list>]. This list includes the start character ^ and sS and tT and whitespace \\s

    (?<=[^^\\ssStT])

    The look-ahead will do a similar check, but only verify the next character is not sS

    (?=[^sS])

    Putting this all together:

    String test = "Is Mississippi a State where there are a lot of Systems?";
    System.out.println(test
            .replaceAll("(?<=[^^\\ssStT])s(?=[^sS])","t")
            .replaceAll("(?<=[^^\\ssStT])S(?=[^sS])","T")
    );