How do you write expression in Label field to replace string with values ?

5034
16
01-15-2016 02:49 PM
PROBERT68
Frequent Contributor

Hi,

Here I have a field that has ID which show each road numbers ie: 200.B, 787, JEF-83N and they are all mixed up..

How do I write to replace them with text with just numbers

Ie:  2000. B  to 200

      JEF-83N  to 83N

I am stuck here ?????

This is to display road numbers to show numbers within the Symbol  Like this , ,

I'm using Label expression with VBscrpit but can take JScrpit or Python  ....

Thanks !!!

0 Kudos
16 Replies
ChrisDonohue__GISP
MVP Alum

Conceptually, the first idea that comes to mind would be to cast them to an integer field, which would theoretically drop out all the characters.   I'll have to tinker with that....

Also, I'm fairly sure there is a way to do this in Python, but don't know the specifics.  I'm sure there will be several replies soon here given the high level of Python skill on GeoNet.

Chris Donohue, GISP

0 Kudos
DarrenWiens2
MVP Honored Contributor

You need to rephrase your question to be as stupid as a computer. You're not looking for "numbers", you're looking for something that meets a set of rules that you've made up.

For example, "if the road label contains a hyphen, I only want the characters after the hyphen"

>>> string = 'JEF-83N'
... if '-' in string:
...     road_number = string.split('-')[1]
... print road_number
... 
83N

...or "if the road label contains a period, I only want the characters before the period"

>>> string = '2000.B'
... if '.' in string:
...    road_number = string.split('.')[0]
... print road_number
...
2000

^ I assume in your example "2000. B  to 200" you meant 2000.

PROBERT68
Frequent Contributor

Yes correct  2000.B to 2000.

0 Kudos
ChrisDonohue__GISP
MVP Alum

Robert - do you have other examples of the "before" and "after"?  I ask as in one of the two examples you provided there is still a non-number in the desired result:

EF-83N  to 83N

What Darren Wiens suggested looks like it will do the trick for some of this, but I suspect there will be a bit more complexity that may require some other Python functions.  If you have a sample set of the start/desired result, that will help in figuring out the exact functions needed.  Essentially, as Darren mentions, you need to come up with the "rules".  Once the "rules" are assembled, the next step will be to come up with the functions that accomplish them.

Also, my cast idea didn't pan out.

Chris Donohue, GISP

0 Kudos
DanPatterson_Retired
MVP Emeritus

Definitely the rules...

Not a solution for all cases!!!!

>>> import string
>>> numbers = string.digits
>>> a = "JEF-83N"
>>> b = "".join([i for i in a if i in numbers])
>>> b
'83'

Although it looks like a solution, it will fail if a = "JEF-83N1" because you will probably not want the appending 1.  Also b is returned as a string...is that what you want ? probably.

So the points that Chris and Darren are trying to make, is that you need to establish the rules.  The point I will make, is that it may be easier to provide a two-step solution rather than finding the "panacea script".

PROBERT68
Frequent Contributor

I'll try that and see if that works. Wish me luck !

0 Kudos
ChrisDonohue__GISP
MVP Alum

This is GIS - what could possibly go wrong? 

Chris Donohue, GISP

PROBERT68
Frequent Contributor

Last Friday, I was probably not thinking correctly. I came down a cold and wasn't thinking so when I wrote this post before I went home.

So, I haven't done anything since then. I will let you know how that works.

Thanks all.

P.S.  Is that related to set of rules or related to label expression ?

I am going to do this anyway near in the future...

0 Kudos
RobertBorchert
Frequent Contributor III

To much thought going into this thread. 

Do it the simple way.

Simply create a new string column to label from.  Use a string because an integer column will always have at least a zero.

Go into MS Access and calculate then number column from the name column

Val([Roads]![STR_NAME])

Then simply find and replace a whole cell values of Zero with a <Null>