[GIS] Creating label formatted like ” 1,000′ ” from numeric field

field-calculatorfields-attributespyqgisregex

This question builds on a previous question I came across at gis.stackexchange:
How to easily edit attribute data using Regular Expressions?

Perhaps someone can point me in the right direction?

I have a vector shapefile of contour lines. In the attribute table I have already converted a field called FEET consisting of integer data containing elevation values to a string field called Label. Additionally I successfully appended a ' to the end of the string so that when the data is labeled it will be read as feet.

The next thing I am attempting to do is to insert a comma between the first two values of the string only if the string length is greater than 4, or so that a comma will only appear between the thousands and hundreds digits in the Label field. The length has to be greater than 4 because I'm also counting the '

My code so far is:

case when  length( "Label") >4  then regexp_replace( "Label", '^([0-9])+([0-9]{3}\')$','$1 , $2') else "Label" end

however the regexp_replace function doesn't seem to accept the group variables $1 $2 in the 3rd parameter of the function.

I've looked at http://docs.python.org/2/library/re.html as the field-calculator regex is from what I understand based on python's use of regex but haven't been able to figure this out. Maybe what I'm attempting to do isn't possible yet with this function in field calculator in QGIS 1.8 on mac OSX. Or more likely my syntax is wrong as I'm a novice programmer and somewhat new to regular expressions.

Best Answer

QGIS has created a function called format_number to address this. You simply do format_number(12345,0) to get 12,345.