如何在 pgsql regexp_replace 中回顾积极的工作?
how can lookbehind positive work in pgsql regexp_replace?
我做过的最简单的测试是这个:
SELECT regexp_replace((09,09,41,41,42,42,49,49,49,49,200,200,400,400,500,500), '(?<=,|^)([^,]*)(,\1)+(?=,|$)', '\1') AS lignes
我想要 regex101 显示的内容:09,41,42,49,200,400,500。
但是整个字符串都来了。
有帮助吗?
首先,除非 standard_conforming_strings
开启,否则不需要加倍反斜杠:
standard_conforming_strings (boolean)
This controls whether ordinary string literals ('...') treat backslashes literally, as specified in the SQL standard. Beginning in PostgreSQL 9.1, the default is on (prior releases defaulted to off). Applications can check this parameter to determine how string literals will be processed. The presence of this parameter can also be taken as an indication that the escape string syntax (E'...') is supported. Escape string syntax (Section 4.1.2.2) should be used if an application desires backslashes to be treated as escape characters.
接下来需要使用g
全局修饰符来替换所有匹配,见9.7. Pattern Matching section:
Flag i
specifies case-insensitive matching, while flag g
specifies replacement of each matching substring rather than only the first one.
使用
SELECT regexp_replace('09,09,41,41,42,42,49,49,49,49,200,200,400,400,500,500', '(?<=,|^)([^,]*)(,)+(?=,|$)', '', 'g') AS lignes
参见online demo。
如果你想让模式更高效,使用双重否定:
'(?<![^,])([^,]*)(,)+(?![^,])'
我做过的最简单的测试是这个:
SELECT regexp_replace((09,09,41,41,42,42,49,49,49,49,200,200,400,400,500,500), '(?<=,|^)([^,]*)(,\1)+(?=,|$)', '\1') AS lignes
我想要 regex101 显示的内容:09,41,42,49,200,400,500。 但是整个字符串都来了。 有帮助吗?
首先,除非 standard_conforming_strings
开启,否则不需要加倍反斜杠:
standard_conforming_strings (boolean)
This controls whether ordinary string literals ('...') treat backslashes literally, as specified in the SQL standard. Beginning in PostgreSQL 9.1, the default is on (prior releases defaulted to off). Applications can check this parameter to determine how string literals will be processed. The presence of this parameter can also be taken as an indication that the escape string syntax (E'...') is supported. Escape string syntax (Section 4.1.2.2) should be used if an application desires backslashes to be treated as escape characters.
接下来需要使用g
全局修饰符来替换所有匹配,见9.7. Pattern Matching section:
Flag
i
specifies case-insensitive matching, while flagg
specifies replacement of each matching substring rather than only the first one.
使用
SELECT regexp_replace('09,09,41,41,42,42,49,49,49,49,200,200,400,400,500,500', '(?<=,|^)([^,]*)(,)+(?=,|$)', '', 'g') AS lignes
参见online demo。
如果你想让模式更高效,使用双重否定:
'(?<![^,])([^,]*)(,)+(?![^,])'