Thread: Substring Creation, how to find last space before x and space or specific character after x

1. Substring Creation, how to find last space before x and space or specific character after x

I've been banging my head against a wall and spent a couple hours searching online to try to figure out how to accomplish this with no luck. I am needing to make a cross reference table of customer part numbers to our part number. Since on each order, we enter the customer supplied part number in a note field, we do have a way to extract this data to create the reference table, however I'm struggling with some of the details.

Previously the script was written to find a string, start there and count out 10 characters. However this does not work in all cases. For instance, the string we are looking for, lets call it "0000" will be present in ALL of the customer part numbers, with the complete part number looking like "0000-00000".

In this instance that method works, however the customer part number could also look like:
"xxx-0000-00000" or even "0000-00000XXX", in which case we would be missing the beginning or end of the part number.

So, I need to be able to find the "0000" and then go back to the last space then forward one to find the starting character, and for the ending character find the "0000" then move forward until I find a space, ., or line return and then go back one to find the ending character of the string. This is what I am having trouble figuring out how to accomplish.

How exactly do I go about defining start_pos and end_pos as I have explained them?

note = odn.od_packing_slip_note
cust_pn = at2("0000",note,1)
start_pos = cust_pn then go back to last space and forward one
end_pos = # of characters from the st_pos until the next space, ., or line return

if cust_pn > 0 then
xref_no = SUBSTR(note,st_pos,end_pos)
end if

2. Re: Substring Creation, how to find last space before x and space or specific character after x

You are probably going to have to build your own custom function. You mention "xxx-0000-00000" or even "0000-00000XXX". Should the second example be "0000-00000-XXX"? Do the X's represent characters or can they be digits as well?

Or maybe not. Will this work?

Code:
```note =  "0000-000000"
? ltrim(regex_merge(note,"[a-z]","","SI"),"-")
= "0000-00000"
note =  "XXX-0000-000000"
? ltrim(regex_merge(note,"[a-z]","","SI"),"-")
= "0000-00000"
note = "0000-00000XXX"
? ltrim(regex_merge(note,"[a-z]","","SI"),"-")
= "0000-00000"```

3. Re: Substring Creation, how to find last space before x and space or specific character after x

0's represent numbers, X's represent characters. In the event that the characters are appended to the end, there would be no "-" to separate them (i.e., no, I did not mean "0000-00000-XXX").

The number of characters will always be the same and always be in the "0000-00000" format, its just a matter of there may or may not be "XXX-" before them, and may or may not be "XXX" after them.

If it helps, the first four characters of the number string are always the same (e.g., "1234"). Every part number will have the first four characters of the number string be "1234".

4. Re: Substring Creation, how to find last space before x and space or specific character after x

Originally Posted by Stan Mathews
.......

Or maybe not. Will this work?

Code:
```note =  "0000-000000"
? ltrim(regex_merge(note,"[a-z]","","SI"),"-")
= "0000-00000"
note =  "XXX-0000-000000"
? ltrim(regex_merge(note,"[a-z]","","SI"),"-")
= "0000-00000"
note = "0000-00000XXX"
? ltrim(regex_merge(note,"[a-z]","","SI"),"-")
= "0000-00000"```
This appears to be doing what we already have, which is extracting only the numeric string. We need to be able to pull the characters before and after the number string as well if they exist.

5. Re: Substring Creation, how to find last space before x and space or specific character after x

Ok, this?

Code:
```note = "abc xyz-0000-00000, next part" + crlf()+" test"
note = strtran(keywords(note)," ",crlf())
keynote = *for_each(wd,wd,note,"-" \$ wd)
? keynote
= xyz-0000-00000

note = "abcdef. 0000-00000, next part" + crlf()+" test again"
note = strtran(keywords(note)," ",crlf())
keynote = *for_each(wd,wd,note,"-" \$ wd)
? keynote
= 0000-00000

note = "abcdef, 0000-00000mmm. another part." + crlf()+" test third time"
note = strtran(keywords(note)," ",crlf())
keynote = *for_each(wd,wd,note,"-" \$ wd)
? keynote
= 0000-00000mmm```
Which would serve as the basis for your custom function.

Or you could nest the expressions.

Code:
```note = "nested test, 0000-00000mmm. another part." + crlf()+" test third time"
keynote = *for_each(wd,wd, strtran(keywords(note)," ",crlf()),"-" \$ wd)
? keynote
= 0000-00000mmm```
Just noticed that would fail to return the proper value if there are other dashes in the note.

Code:
```note = "nested-test, 0000-00000mmm. another part." + crlf()+" test third time"
keynote = *for_each(wd,wd, strtran(keywords(note)," ",crlf()),"-" \$ wd .and. (isnumber(word(wd,1,"-")) .or. isnumber(word(wd,2,"-") ) ))
? keynote
= 0000-00000mmm
note = "nested-test, zzz-0000-00000. another part." + crlf()+" test-third time"
keynote = *for_each(wd,wd, strtran(keywords(note)," ",crlf()),"-" \$ wd .and. (isnumber(word(wd,1,"-")) .or. isnumber(word(wd,2,"-") ) ))
? keynote
= zzz-0000-00000```

6. Re: Substring Creation, how to find last space before x and space or specific character after x

Thank you so much.

From what I can see after running a few tests using our actual data, this is exactly what we need.

I resolved the issue with returning other dashes in the note field by replacing it with the portion of numbers we know will always be present within the part number.

7. Re: Substring Creation, how to find last space before x and space or specific character after x

Hi Amanda,

I know I'm a bit late, but would
Code:
`cust_pn= if(occurs("-",note)=2,substr(note,at("-",note,2)+1,5),substr(note,6,5))`
work?

Here are a few examples :
note = "0000-12345xxx"
cust_pn= if(occurs("-",note)=2,substr(note,at("-",note,2)+1,5),substr(note,6,5))
?cust_pn
= "12345"
note = "xxx-0000-54321"
cust_pn=if(occurs("-",note)=2,substr(note,at("-",note,2)+1,5),substr(note,6,5))
?cust_pn
= "54321"
note = "0000-34567"
cust_pn= if(occurs("-",note)=2,substr(note,at("-",note,2)+1,5),substr(note,6,5))
?cust_pn
= "34567"

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•