简体   繁体   中英

REGEXP_REPLACE for name and surname masking

select REGEXP_REPLACE('Tina Frederich Piedro', '\w+', '*') from table;

I'm using \\w+ this but it returns * * * what is the true regex for expected output?

Input;

Tina Frederich Piedro

Expected Output;

T*** F******** P*****

This is not a general solution, but it might work in your case. You can replace the lower case letters with '*' s:

select REGEXP_REPLACE('Tina Frederich Piedro', '[a-z]', '*', 1, 0, 'c')

The 'c' is for a case-sensitive replace.

I'm by no means a regexp expert, I had to split the answer into two stages. I imagine someone more capable can combine the two steps. But this does the trick and also accounts for upper case letters in the middle of names or punctuation for example O'Brian.

select
     regexp_replace(lowers_done,'\*[A-Z]','**') first_letters_only
from

(
select
    regexp_replace('Tina McDonald O''Brian','[a-z]|[[:punct:]]','*') lowers_done
from
    dual
)

Output:

T*** M******* O******

If it's for security reasons you can use the DBMS_REDACT package to apply masking pattern on sensitive information. Documentation is here

I'm aware it's not a regexp solution though and further more this functionality may be subject to additional licencing from oracle, but that the solution Oracle suggest to PCI compliant solution on sensitive data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM