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
  • 75,746
  • 7
  • 171
  • 282
draganche85
  • 91
  • 1
  • 3

2 Answers2

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.

Jacob F
  • 964
  • 4
  • 8
  • +1 Regular expressions can tackle the most challenging string manipulation. – Aaron Jun 16 '16 at 03:39
  • Sorry, I didn't mention that I use Qgis... and I can't reach how this script work in a qgis field calculator – draganche85 Jun 16 '16 at 09: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 01: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 Jun 17 '16 at 14:56
  • Thank you Jacob F, your method work excellent! I was tried first with regexp_substr and problem with asterisk. Good to know for that modification. Thanks. – draganche85 Jun 20 '16 at 18:27
  • @draganche85 - Don't forget to click the green faded tick on the left-hand side of this answer to formally accept it as having solved your problem :) – Joseph Jun 21 '16 at 09:29
  • Using your code as a starting point I modified it so that you can define the string and index value as part of the function like so; splitField("MYFIELD",'x',n) x being the desired string and y being the desired list index number (+- can be used for forward/backwards indexing). For details https://gis.stackexchange.com/questions/363695/adding-vaiables-to-python-functions – MrKingsley Jun 02 '20 at 16:05
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

Joseph
  • 75,746
  • 7
  • 171
  • 282
  • 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. – draganche85 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 09:28