I have strings like this:


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
    Have you looked at regular expressions? regexp_substr in the field calculator?
    – Fezter
    Commented Jun 15, 2016 at 23:21

2 Answers 2


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


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


Set your output field.

Then click OK to run.

  • +1 Regular expressions can tackle the most challenging string manipulation.
    – Aaron
    Commented Jun 16, 2016 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 Commented Jun 16, 2016 at 9:27
  • I updated my answer with a method for QGIS.
    – Jacob F
    Commented Jun 16, 2016 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
    Commented Jun 17, 2016 at 1:07
  • 3
    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
    Commented Jun 17, 2016 at 14:56

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:


  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:


  • 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. Commented Jun 20, 2016 at 18:22
  • @draganche85 - Most welcome but I also agree in that Jacob's answer is much better =)
    – Joseph
    Commented Jun 21, 2016 at 9:28

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.