2

I tagged all my drawing objects with a identifier which includes square brackets. eg [302] [303] [509].

Edit to add further information.

  • Not all objects are tagged with brackets.
  • A few are tagged with just numbers eg 425, 359.
  • A few are tagged Furrow
  • A few are tagged Z
  • a few are tagged I
  • A few are tagged G?
  • A few are tagged multiple ditches
  • There may be a few others sometimes.

Is there a code which could read the [number] from the text field and paste the number (without the brackets) into the numerical field?

Edit - if possible the tags that are already numbers would transfer as they are, while any purely text can be left blank.

If there is such a code, how and where would I input it?

1
  • Dont name the new field "2". Try "field2" or something.
    – Bera
    Commented Apr 18, 2018 at 10:26

1 Answer 1

3

A bit ugly, but this should work in the field calculator:

toint( replace(replace("yourfield", ']', ''), '[', '') ) 

This will create an numeric (integer) field with the number between the brackets.

Edit: I believe this expression will work for your tags:

if(regexp_substr( "input", '(\\d+)') <> '', toint(regexp_substr("input", '(\\d+)')), NULL)

This converts :

  • [number] to number,
  • number to number,
  • any text to NULL,
  • NULL to NULL.
11
  • Any solution will do. I just assumed it might involve Python. I can't even duplicate a field in the field calculator, and have no idea how to use your code. Sorry, and thanks. How do I tell it which attribute to copy and where to paste the amended version? Commented Apr 18, 2018 at 9:21
  • Well, there is a nice tutorial on QGIS website which explains how to use it. You just have to copy the expression I wrote into the "Expression" field, replace yourfield with the name of the text field containing the number with bracket, and choose the result field name in "Output field name".
    – ArMoraer
    Commented Apr 18, 2018 at 9:28
  • I've managed to make a copy of the field (as a text field and with all the numbers) . I relaced yourfield in the code with that field name, but the calculator says that the expression is invalid. The new field is named 1 and I used this code toint( replace(replace("1", ']', ''), '[', '') ) Commented Apr 18, 2018 at 9:29
  • 1
    This could definitively be the issue ;) Could you please add to your question a copy of your objects tags (or a representative part of it if the list is too long)? There is probably a way to deal with the variety of input formats, using a different expression than the one I came up with.
    – ArMoraer
    Commented Apr 18, 2018 at 10:06
  • 1
    I updated my answer. Let me know if there is still any issue.
    – ArMoraer
    Commented Apr 18, 2018 at 12:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.