6

I have strings like this:

234/1-1
3345#5
11/2~10
6754*15

It needed only rightmost part of string after '-', '#', '~'or '*' . I figured how to extract when there is only one sign for example '~' with right and strpos. Can I do that with all at once with field calculator?

1
  • 1
    Have you looked at regular expressions? regexp_substr in the field calculator?
    – Fezter
    Jun 15 '16 at 23:21
11

ArcGIS Method

In field calculator switch the parser to Python and check the "Show Codeblock" box.

In the Pre-Logic Script Code box enter

import re
def splitField(inField):
    return  re.split('[-|#|~|\*]', inField)[1]

In the TEXT box enter

splitField(!nameofField!)

QGIS Method

Open Field Calculator and go to the Function Editor tab.

In the code pane enter

import re
from qgis.core import *
from qgis.gui import *

@qgsfunction(args="auto", group='Custom')
def splitField(inField, feature, parent):
    return  re.split('[-|#|~|\*]', inField)[1]

Click the load button.

Go to the Expression tab and enter

splitField("NAMEOFFIELD")

Set your output field.

Then click OK to run.

8
  • +1 Regular expressions can tackle the most challenging string manipulation.
    – Aaron
    Jun 16 '16 at 3:39
  • Sorry, I didn't mention that I use Qgis... and I can't reach how this script work in a qgis field calculator Jun 16 '16 at 9:27
  • I updated my answer with a method for QGIS.
    – Jacob F
    Jun 16 '16 at 16:01
  • 2
    No need for a custom function this works: regexp_substr('11/2~10','[-|#|~|\*](.*)') regexp_substr is a built in function.
    – Nathan W
    Jun 17 '16 at 1:07
  • 2
    Good to know, although I had to modify the statement to regexp_substr('11/2~10','[-|#|~|\\*](.*)') to get it to work with the asterisk.
    – Jacob F
    Jun 17 '16 at 14:56
3

Hmm not sure how to do it all at once but you can do it in a couple of steps. I made an example using the values you specified:

Example


  1. First we need to replace all forward slashes with an underscore _, the reason for this will be explained in the next step. You can use the expression:

    replace( "name", '/', '_')
    

    Field calculator to replace


  1. Now we can extract the strings after any non-ASCII symbols such as those you mentioned. We can do this a varierty of operations:

    • substr - returns a part of a string
    • strpos - return first matching position of a substring

    We will also use the special regular expression operation \\W which matches any non-alphanumeric character (i.e. anything other than letters, numbers, ^ and _). This is why we needed to replace / with _. Combining all the above allows us to extract the string after a non-alphanumeric symbol:

    substr( "replace", strpos( "replace", '\\W+')+1, strpos( "replace", '\\W+'))
    

    Field calculator to extract

We also use +1 because we do not want to include the non-alphanumeric symbol itself, only everything after it.


  1. Result:

    Result

2
  • 1
    This was method I was thinking about, but I used Jacobs code which is shorter and work great with my data. Thank you on your answer. Jun 20 '16 at 18:22
  • @draganche85 - Most welcome but I also agree in that Jacob's answer is much better =)
    – Joseph
    Jun 21 '16 at 9:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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