postgresql - Seemingly incorrect regex evaluation in regexp_replace -
i stumbled upon curious behavior of regexp_replace
postgresql function. looks bug doubt myself first. when run
select regexp_replace(e'1%2_3', '([_%])', e'\\ \\1', 'g')
it correctly prefixes either underscore or percent backslash+space , produces "1\ %2\ _3
". when remove space (it doesn't have space, can character)
select regexp_replace(e'1%2_3', '([_%])', e'\\\\1', 'g')
it stops using captured parenthesized expression in substitution , produces "1\12\13
" instead of "1\%2\_3
". appreciate if tell me doing wrong. need add backslash before characters in string.
update: able achieve desired behavior running
select regexp_replace(e'1%2_3', '([_%])', e'\\\\\\1', 'g')
my original example still seems bit illogical , inconsistent. inconsistency using same e'...'
syntax 4 backslashes may produce different result.
in second query, after backslash escapes processed @ string level, have replacement string \\1
.
what's happening escaped backslash prevents \1
being recognized back-reference. need set of backslashes, replacement string \\\1
literal backslash , back-reference. since every literal backslash needs escaped, need double of them.
select regexp_replace(e'1%2_3', '([_%])', e'\\\\\\1', 'g')
Comments
Post a Comment