Skip to main content
Jul 19, 2013 10:52 AM UTC
Question

Extract numbers from string in creo

Have parameter that is 914klm5ft12or3467 that is a string

i want to filter numbers and put in a parameter that is REAL like 9145123467

 

Any suggestions?

19 replies

Jul 29, 2013 9:25 PM UTC

exactly how does the parameter evaluate?

Jul 30, 2013 9:27 AM UTC

this parameter evaluates some extracted values from tables "extract(TABLE_1,1,3)", "extract(TABLE_1,3,6)"

in old files case evaluates the modelname parameter (that is a string) and compose with extracts the "NUMBERCAD" parameter that drivers the numbering in pdm

in othe case i have to extract only numbers to acquire gps coordinates or absolute coordinates (that are comma separated values)

all those things are acquired as strings and shall be filtered and turned in to numbers

Jul 29, 2013 9:28 PM UTC

Are the occurences of numbers in that string random? Is the string's length also random?

Jul 30, 2013 1:09 AM UTC

I can only see this in an extensive -IF- clause that evaluates each entry in the string as a valid "numeric" string that represents a number and next, determine the length of the full number string... and create variables, again based on an extensive -IF- to determine the value of each string"number".

Something like:

(capture every character as a variable)

(evaluate every variable purging alpha characters)

...

if A=="1"

AN=1

endif

if A=="2"

AN=2

endif

...

(Determine the string length)...

(multiply each variable by 10,100, 1000, 10000, 100000, etc...)

(add all the variable together)

Would I want to write this? NOPE!

We need a simple function that will parce a value from a text string by purging non-numeric characters.

As for the OP's request, I would reverse the requirements by making the string from the values rather than making the value from the string.

Jul 30, 2013 9:42 AM UTC

NUMBERCAD=rel_model_name

than from this parameter "NUMBERCAD" that is a string (because model_name is a string) try to filter and get only numbers.

in creo relation there in no way to extract FROM a string TO a number, but is only possible to do the opposite with this command "extract(PARAMETER,3,6)" (extract from third value of parameter 3 following values)

also i should reverse the requirements but you can't drive new file name in creo and you can't put filters on creation

Oct 29, 2016 10:38 PM UTC

I'd base it on something like this:

/*Without comments

digitlist = "0123456789"

value = 0

digit_found = search(digitlist,extract(rel_model_name(),len(rel_model_name)-0,1)-1

value = value+digit_found

digit_found = search(digitlist,extract(rel_model_name(),len(rel_model_name)-1,1)-1

value = value+digit_found*10

digit_found = search(digitlist,extract(rel_model_name(),len(rel_model_name)-2,1)-1

value = value+digit_found*100

digit_found = search(digitlist,extract(rel_model_name(),len(rel_model_name)-3,1)-1

value = value+digit_found*1000

d0=value

This was to clip numbers at the ends of parameters. In your case I would have the logic start with the first one and use

IF digit_found <> 0

value = value*10 + digit_found.

ENDIF

Then wrap the digit_found ... and IF digit_found ... in an

IF len(parameter)> whatever_step

digit_found ...

IF digit_found...

value = ...

ENDIF

ENDIF

Repeat for whatever_step = 1 to whatever the maximum number of characters you expect.

Since it's an integer, ITOS() should work OK if you need a string.

Oct 24, 2017 9:53 AM UTC

This was exactly what I was looking for. In my case, I wanted to convert the last 3 characters (string) from our part name to a number.

 

I have shortened the code just a little bit more:

 

DIGITS = "0123456789"
TEMP = 0
TEMP = TEMP+(SEARCH(DIGITS,EXTRACT(REL_MODEL_NAME,STRING_LENGTH(REL_MODEL_NAME)-0,1))-1)*1
TEMP = TEMP+(SEARCH(DIGITS,EXTRACT(REL_MODEL_NAME,STRING_LENGTH(REL_MODEL_NAME)-1,1))-1)*10
TEMP = TEMP+(SEARCH(DIGITS,EXTRACT(REL_MODEL_NAME,STRING_LENGTH(REL_MODEL_NAME)-2,1))-1)*100

 

You can extend this for as long as you like.

 

Just one warning, there is no error checking!

Oct 24, 2017 1:13 PM UTC

Keep in mind that you're going to get a negative 1 any time this encounters something other than a number (anything not present in DIGITS.)