简体   繁体   English

使用Postgres regexp_replace屏蔽查询字符串参数值

[英]Masking a query string param value using Postgres regexp_replace

I want to mask movie names with XXXXXXXX in a PostgreSQL table column. 我想用PostgreSQL表列中的XXXXXXXX屏蔽电影名称。 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模式之后)

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 . 我想将所有电影名称都替换为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. 该正则表达式不起作用,因为(.*?)(&?)匹配空字符串,或者如果立即遵循hollywood_genre_movieTitle\\d+=模式,则&落入组3。

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. 您需要使用否定的字符类[^&]+量词来匹配hollywood_genre_movieTitle\\d+=模式之后的&以外的任何1个或多个字符。

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\\d+=) -捕获组1:
    • hollywood_genre_movieTitle - a substring hollywood_genre_movieTitle子字符串
    • \\d+= - 1 or more digits and a = after them \\d+= -1个或多个数字,后跟一个=
  • [^&]+ - 1 or more chars other than & . [^&]+ - &以外的1个或更多字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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