I have this expression in the field calculator:
case
when ident ~ '^B\\d+/\\d+' then regexp_replace(ident, '^B(\\d+)/(\\d+)', 'Hb' || rpad('\\1', 3, '0') || rpad('\\2', 3, '0'))
when ident ~ '^\\w\\w' then regexp_replace(ident, '^(\\w+)/?(\\d+)','H' || lpad('\\1', 3, '_') || rpad('\\2' , 3, 0))
end
theregexp_replace('1', '^(\\d+)', rpad('\\1', 3, '0') )
gives pad01
functions appear to be behaving erratically, here are a bunch of inputs and outputs:
"WV6" "HWV_06"
"B4/2" "Hb0402"
"B3/10" "Hb03010"
"LWV1" "HLWV_01"
Note that the padding is inconsistent in a number of different ways! In the case of the zero padding the padding is one character lessrather than specified, except when the original string is two characters and then we get the001
as expected number.
with the '' padding I get the correct behaviour when there are two chars in the source but when there are three it still add an '' when it should not.
The behaviour is systematicthe *pad functions work fine when called outside regexp_replace
Weird! This has to be a bug? or am I missing something?
If it is a bug I'll log something in the issue tracker.
BTW I was delighted when I found I could use complex expressions including functions in the third parameter of regexp_replace
! kudos to the QGIS developers!