Data redaction with regex in Oracle I would like to redact data with regex in oracle database. I know the procedure but I get unpredictable results.

I use the following script to apply redaction to a certain column with card numbers. 

BEGIN
DBMS_REDACT.ADD_POLICY ( 
   object_schema          =&gt; 'my_schema',
   object_name            =&gt; 'my_table',
   column_name            =&gt; 'card_number',
   policy_name            =&gt; 'redact_my_table', 
   function_type          =&gt; DBMS_REDACT.REGEXP,
   function_parameters    =&gt; null,
   regexp_pattern         =&gt; '(............)(.+)(........)',--DBMS_REDACT.RE_PATTERN_CC_L6_T4,
   regexp_replace_string  =&gt; '\1****\3', --dbms_redact.re_redact_ccn, --DBMS_REDACT.RE_REDACT_CC_MIDDLE_DIGITS,
   regexp_position        =&gt; DBMS_REDACT.RE_BEGINNING,
   regexp_occurrence      =&gt; DBMS_REDACT.RE_FIRST,
   regexp_match_parameter =&gt; 'i',
   policy_description     =&gt; 'Regular expressions to redact credit card numbers',
   expression             =&gt; 'SYS_CONTEXT(''USERENV'',''SESSION_USER'') != ''my_schema'''
   );
END;
/


Expected result would be first 6 and last 4 unmasked like 123456****1234, but I get this instead:



From the documentation I would suppose the below script would work by default, but it performs a full redaction instead of just masking some chars. I get a whitespace per row.

BEGIN
DBMS_REDACT.ADD_POLICY ( 
   object_schema          =&gt; 'my_schema',
   object_name            =&gt; 'my_table',
   column_name            =&gt; 'card_number',
   policy_name            =&gt; 'redact_my_table', 
   function_type          =&gt; DBMS_REDACT.REGEXP,
   function_parameters    =&gt; null,
   regexp_pattern         =&gt; DBMS_REDACT.RE_PATTERN_CC_L6_T4,
   regexp_replace_string  =&gt; DBMS_REDACT.RE_REDACT_CC_MIDDLE_DIGITS,
   regexp_position        =&gt; DBMS_REDACT.RE_BEGINNING,
   regexp_occurrence      =&gt; DBMS_REDACT.RE_FIRST,
   regexp_match_parameter =&gt; 'i',
   policy_description     =&gt; 'Regular expressions to redact credit card numbers',
   expression             =&gt; 'SYS_CONTEXT(''USERENV'',''SESSION_USER'') != ''my_schema'''
   );
END;
/


Database used is Oracle Database 12c Enterprise Edition Release 12.1.0.2.0

This are the constants in DBMS_REDACT package:

RE_PATTERN_CC_L6_T4        CONSTANT   VARCHAR2(33) := '(\d\d\d\d\d\d)(\d\d\d*)(\d\d\d\d)';
RE_REDACT_CC_MIDDLE_DIGITS CONSTANT   VARCHAR2(10) := '\1XXXXXX\3';


If i put the \d in regex patter i always get full redaction. Using dot i managed to get the scrambled data from the image.

If anyone could try here is also the drop statement:

exec dbms_redact.drop_policy('my_schema','my_table','redact_my_table');


Any ideas what am I doing wrong?
Thanks to @mustaccio's question got me thinking about datatype issues. 

NVarchar2 seems to be the issue in this problem. 

Creating a copy of the column in varchar2 fixed the issue. Data redaction on varchar2 is working as expected.