6

I´m working with QGIS 2.12. Is it possible to split string "from the right side" (it means I needed the last text from every field)?

In one field I have description like:

description
new street 25
old street 2
lower 26A
lower new street 125
Jozefs and Elisabeths new gardens 147A

Every line has a different count of texts. I needed a column with numbers:

numbers
25
2
26A
125
147A

Could be possible in QGIS?

Taras
  • 32,823
  • 4
  • 66
  • 137
lucy147
  • 71
  • 1
  • 5
  • Sorry for continuing on this question here but I am not allowed to comment yet. I would like to do the opposite, that is: new street 25 = new street old street 2 = old street lower 26A = lower lower new street 125 = lower new street Jozefs and Elisabeths new gardens 147A = Jozefs and Elisabeths new gardens – Nedim Oct 12 '17 at 11:04

2 Answers2

9

You can do it using the following formula in the Field Calculator:

regexp_substr("Field_Name",'(\\d+|\\d+.+)') 

Where:

The first \ is to escape \d

\d+ : means extract one or more digits.

| : means OR.

\d+.+ : means extract one or more digits and one or more any other character.

It will give you the following results:

numbers
25
2
26A
125
147A

Even if you have text with a name 'Text 123456789ABCDEF', the output will be:

123456789ABCDEF

Taras
  • 32,823
  • 4
  • 66
  • 137
ahmadhanb
  • 40,826
  • 5
  • 51
  • 105
  • Thank you very much for your help, but the syntax does not work properly. If the last text is shorter than 2 digits (old street 1, new street 5....), than in the new column is written "NULL". The last digits (number of the house) could be combination: 1 / 1A / 11 / 11A / 100 / 100A. thank you – lucy147 Jul 11 '16 at 11:13
  • I updated my answer to solve the problem if the last text is shorter than 2 digits. – ahmadhanb Jul 12 '16 at 00:53
  • @ahmadhanb What do these (\d+|\d+.+) characters mean in your expression? – Skye Jul 28 '16 at 08:47
  • 1
    @Skye "\d+" means extract one digit or more digits, but the first \ is to escape \d. "|" means OR. "\d+.+" means extract one or more digits and one or more any other character. – ahmadhanb Jul 28 '16 at 09:06
4

In the Field Calculator:

right("column", strpos("column", ' '))

Should do the job.

Taras
  • 32,823
  • 4
  • 66
  • 137
dmh126
  • 6,732
  • 2
  • 21
  • 36
  • Thank yo very much for your answer, but the syntax does not work properly in every field. the results were 25 - r 26A - s 147A - t 125 - t 2. PC added the last letter in many fields..... – lucy147 Jul 11 '16 at 11:05