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?
-
1Have you looked at regular expressions? regexp_substr in the field calculator?Fezter– Fezter2016年06月15日 23:21:36 +00:00Commented 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
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.
-
+1 Regular expressions can tackle the most challenging string manipulation.2016年06月16日 03:39:36 +00:00Commented 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 calculatordraganche85– draganche852016年06月16日 09:27:53 +00:00Commented Jun 16, 2016 at 9:27
-
I updated my answer with a method for QGIS.Jacob F– Jacob F2016年06月16日 16:01:56 +00:00Commented Jun 16, 2016 at 16:01
-
2No need for a custom function this works:
regexp_substr('11/2~10','[-|#|~|\*](.*)')
regexp_substr
is a built in function.Nathan W– Nathan W2016年06月17日 01:07:56 +00:00Commented Jun 17, 2016 at 1:07 -
3Good 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– Jacob F2016年06月17日 14:56:28 +00:00Commented 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:
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", '/', '_')
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+'))
We also use +1
because we do not want to include the non-alphanumeric symbol itself, only everything after it.
Result:
-
1This 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– draganche852016年06月20日 18:22:27 +00:00Commented Jun 20, 2016 at 18:22
-
@draganche85 - Most welcome but I also agree in that Jacob's answer is much better =)Joseph– Joseph2016年06月21日 09:28:41 +00:00Commented Jun 21, 2016 at 9:28
Explore related questions
See similar questions with these tags.