3

I have this expression in the field calculator:

regexp_replace('1', '^(\\d+)', rpad('\\1', 3, '0') ) gives 01 rather than 001 as expected.

the *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!

Russell Fulton
  • 2,994
  • 3
  • 31
  • 55

1 Answers1

0

It's not a bug in any of the functions, more an implementation detail that isn't evident at first look.

The expression engine evaluates from inside out, so rpad is run first with no concept of the outer function or the regex.

This is run first:

rpad('\\1', 3, '0')

which it looks at and goes "Ok cool I can pad that for you". The result:

\10

the \\ will escape the \ so you end up with it in your final output. Three characters just like you asked for, well as far as the expression is concerned.

By the time your regexp_replace runs it looks like this:

regexp_replace('3', '^(\\d+)', '\10' )

and at that point regexp_replace is doing what it is told. Replacing the first group with replace string. So you end up with 30.

Nathan W
  • 34,706
  • 5
  • 97
  • 148