简体   繁体   中英

Masking a query string param value using Postgres regexp_replace

I want to mask movie names with XXXXXXXX in a PostgreSQL table column. The content of the column is something like

hollywood_genre_movieTitle0=The watergate&categorey=blabla&hollywood_genre_movieTitle1=Terminator&hollywood_genre_movieTitle2=Spartacus&hollywood_genre_movieTitle3=John Wayne and the Indians&categorey=blabla&hollywood_genre_movieTitle4=Start Trek&hollywood_genre_movieTitle5=ET&categorey=blabla

And I would like to mask the titles (behind the pattern hollywood_genre_movieTitle\\d ) using the regexp_replace function

regexp_replace('(hollywood_genre_movieTitle\d+=)(.*?)(&?)', '\1XXXXXXXX\3', 'g')

This just replaces the first occurrence of a title and and cuts the string. In short this expression does not do the thing I want. What I would like is that all movies names are replace with XXXXXXXX .

Can someone help me solve that?

The regex does not work because (.*?)(&?) matches an empty string or & lands in Group 3 if it immediately follows hollywood_genre_movieTitle\\d+= pattern.

You need to use a negated character class [^&] and a + quantifier to match any 1 or more chars other than & after the hollywood_genre_movieTitle\\d+= pattern.

SELECT regexp_replace(
            'hollywood_genre_movieTitle0=The watergate&categorey=blabla&hollywood_genre_movieTitle1=Terminator&hollywood_genre_movieTitle2=Spartacus&hollywood_genre_movieTitle3=John Wayne and the Indians&categorey=blabla&hollywood_genre_movieTitle4=Start Trek&hollywood_genre_movieTitle5=ET&categorey=blabla',
            '(hollywood_genre_movieTitle\d+=)[^&]+', 
            '\1XXXXXXXX', 
            'g') 

See the online demo .

Details

  • (hollywood_genre_movieTitle\\d+=) - Capturing group 1:
    • hollywood_genre_movieTitle - a substring
    • \\d+= - 1 or more digits and a = after them
  • [^&]+ - 1 or more chars other than & .

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