Search code examples
javaregexregexp-replace

Is there a regex to mask all characters excluding first & last characters in each word


For example, I have below list of words to be masked.

India
Australia
North America
South America
White House building

I need an output like below.

Ixxxa
Axxxxxxxa
Nxxxh Axxxxxa
Sxxxh Axxxxxa
Wxxxe Hxxxe bxxxxxxg

using below expression I'm able to mask last characters including spaces.

return input.replaceAll("[\\s()]+", " ").replaceAll("\\d(?=(?:\\D*\\d){4})","X");

Solution

  • To match a non whitepace char except for the "outer" ones, you can assert a non whitespace char \S to the left and to the right right:

    (?<=\S)\S(?=\S)
    

    In Java

    (?<=\\S)\S(?=\\S)
    

    See a regex demo and a Java demo.

    Java example:

        String regex = "(?<=\\S)\\S(?=\\S)";
        String string = "India\n"
     + "Australia\n"
     + "North America\n"
     + "South America\n"
     + "White House building\n"
     + "a\n"
     + "ab\n"
     + "abc\n"
     + "tthis123_isatest\n"
     + "!@#$%";
    
        String result = string.replaceAll(regex, "x");
        System.out.println(result);
    

    Output

    Ixxxa
    Axxxxxxxa
    Nxxxh Axxxxxa
    Sxxxh Axxxxxa
    Wxxxe Hxxxe bxxxxxxg
    a
    ab
    axc
    txxxxxxxxxxxxxxt
    !xxx%