Sample Header Ad - 728x90

Replace every character in each word, except the first two characters

1 vote
2 answers
2105 views
Due to privacy concerns, I need to hide personal information, while remaining somewhat understandable. I propose to achieve that by hiding everything except the first two characters. The word length should stay the same:
-none
FirstName SecondName LastName -> Fi****** Se******** La******
My attempt is working, but not 100%:
SELECT regexp_replace('Firstname Secondname Lastname', '\y(\S)(\S)','\1' 
       || repeat('*', length('\2')),'g');
Output:
-none
F**rtname S**condname L**stname
With only first character it is easier, and it works, but I need the second character too:
SELECT regexp_replace('Firtname Secondname Lastname', '\Y\w', '*','g');
Output:
-none
F******* S********* L*******
[Demo](https://www.db-fiddle.com/f/4jyoMCicNSZpjMt4jFYoz5/7139)
Asked by oren (13 rep)
Feb 10, 2023, 03:25 PM
Last activity: Aug 20, 2023, 08:45 AM