7

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?

Joseph
76.7k8 gold badges173 silver badges286 bronze badges
asked Jun 15, 2016 at 21:25
1
  • 1
    Have you looked at regular expressions? regexp_substr in the field calculator? Commented Jun 15, 2016 at 23:21

2 Answers 2

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.

answered Jun 16, 2016 at 1:57
8
  • +1 Regular expressions can tackle the most challenging string manipulation. 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. 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. 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. Commented Jun 17, 2016 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

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

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.