Skip to main content
2 of 4
added 753 characters in body
Russell Fulton
  • 3k
  • 3
  • 31
  • 56

Is this a bug in field calculator?

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

the pad 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 less than specified, except when the original string is two characters and then we get the 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 systematic

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!

Russell Fulton
  • 3k
  • 3
  • 31
  • 56