Regexp replace solution in hive to mask first 6 characters

Viewed 336

Trying to mask the first 6 characters in hive using the regexp_replace. But, not able to achieve a solution.

Input :

123-45-6789

Output :

###-##-6789
2 Answers

If you are flexible using other characters other than # to mask the string, you could use an in-built masking function mask_show_last_n.

select mask_show_last_n('123-45-6789',4) 

Output would be nnn-nn-6789 as n is used to mask numbers by default in these masking functions.

Get substring of first 6 characters and replace in it everything except -, then concatenate with the rest of the string:

select regexp_replace(substr('123-45-6789',1,6),'[^-]','#')||substr('123-45-6789',7)

Result:

###-##-6789

For Hive version < 2.2.0 use concat instead of || operator:

select concat( regexp_replace(substr('123-45-6789',1,6),'[^-]','#'), substr('123-45-6789',7))
Related