Select to view content in your preferred language

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

5621
16
01-15-2016 02:49 PM
PROBERT68
Honored 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
MichaelRobb
Honored Contributor

My thoughts on this:

Simply creating fields to satisfy labeling is a DBAs worst nightmare... as well as a developers worst nightmare and an architecture guys worst nightmare.  Any process used for updating the data then need to handle these 'one off's'.  And then How many one offs are to be made? There is no scalablity with this. Its an immediate fix that can make a huge mess in a hurry.  The data should be clean or in this case, if need be, expressions can be made.. its not hard. 

As mentioned above... Rules need to be understood.. then a simple expression codeblock can be derived... and it will work.

Where was it mentioned the poster was using a Personal Geodatabase?

MichaelRobb
Honored Contributor

As already mentioned:

Based on the i.e. supplied. JEF-83N  to 83N How is 83N a numeric value ? (just a number would be 83, excluding the N.

0 Kudos
PROBERT68
Honored Contributor

Michael,

I didn't attribute them. The layer was done by one of the Forest Service where I work for assigned the numbers and I believe they were on the forest road or private load. I haven't looked at them yet...

I had to create a map for one of our Ranger District who need me to make map of a Locator Map of their district ranger where their campground is and the layer I had had multiple road names there. I will work on them tomorrow ( Today  is a holiday for us to take the day off ).

So the field column I have for the roads has many multiple  road numbers like I mention at the top. So, what I am trying to do is show the symbol for each road that I have on the roads. Some of them have private roads, Forest road, Interstate, State and Federal roads. That is why I asked because the number and strings are mixed.

I hope I explained in more clarify this time that I posted from last Friday.

0 Kudos
MichaelRobb
Honored Contributor

HI Robert.

Regardless if you attributed them or not, the statement you said quote "How do I write to replace them with text with just numbers"  and then your example includes you wanting to have an output of 83N.  This is what was confusing. 

Let me clarify:

Are you asking to have an output of labels of JUST NUMBERS?  so then it would only be 83. 

If you want an output of Labels of 83N in the example, then your statement of asking JUST NUMBERS is incorrect.

If you want a label expression to handle anything in a field to only label with Digits only then Check out my post reply to your original Post.

If you are looking at 'changing the original field so for a field calculator.. it is also the same idea, but slightly different... but then that comes to a challenged question of 'why are you creating fields to create labels?'...A separate discussion.

PROBERT68
Honored Contributor

All right, so saying numbers isn't the right word for writing an expression. Instead of numbers should I just say values , or string ?

0 Kudos
DanPatterson_Retired
MVP Emeritus

as in the many examples that have been given, you are trying to extract numbers from a string...

"83" or '83' or 83N  are strings.  The first 2 are overt because they are enlosed in quotes. 83N has to be a string because you cannot have a number beside test because in programming world they are different objects.  As soon as you have an address, you are working with strings. ....so it is terminology that is the key and the many possibilities provide solutions.  I will reiterate again, as I do on these topics, ... don't wait for the "tool" that will work in all cases.  Breaking a problem down into its components, or exceptions will expedite a faster resolution by using several different tools rather than trying to find the all encompassing solutions.   I am sure that your whole problem could have been resolved by using several little "solutions" rather than waiting for the "ONE" solution that will work in all cases.

MichaelRobb
Honored Contributor

Though there is a discrepancy with you asking for Just numbers but in your example, you included a character that is clearly not a number...

Below works... I used a Regular Expression pattern to basically remove ANYTHING except digits.  But this expression can be tailored to anything you would like.

Use this in your label Expression:

1. Check Advanced

2. Parcer = VBScript

TEST = the Field in the attribute table to get string

Function FindLabel ([TEST])

Dim strInputString

Dim strOutputString

strInputString = [TEST]

Dim objRegEx

set objRegEx = createobject("VBScript.RegExp")

objRegEx.Global = True

objRegEx.Pattern ="[^0-9]"

strOutputString = objRegEx.Replace(strInputString,"")

'Return string for Labeling

FindLabel = strOutputString

End Function