Search address string and parse out zip code

Posted on 2008-02-08
Medium Priority
Last Modified: 2013-12-18
Hello -

I am looking to search a string for a zip code.  We have a field in a Notes db that has city, state and zip code combined into one text field.  Through lotusscript I need to isolate the 5 digit zip from the rest of the text.  Since it is a free text field data has been entered inconsistently, so there is no specific delimiter I can look for.  How can I search for 5 consecutive numbers?

Thanks in advance.
Question by:KDrago
LVL 13

Expert Comment

ID: 20854901
I have tried to avoid looping through a string, character by character, keeping track of the length of number fragments, so following code may look different than you'd expect.
It works well though!

It's designed for a button on a form (UIDoc in editmode). I'm sure you'll be able to rewrite that to a NotesDocument-base.
Sub Click(Source As Button)
	Dim Ws As New NotesUIWorkspace
	Dim UIDoc As NotesUIDocument
	Dim Txt As String
	Dim KeepChars As Variant
	Dim DeleteTxt As Variant
	Dim NumTxt As Variant
	Set UIDoc = Ws.CurrentDocument
	Txt = UIDoc.FieldGetText("Txt") ' field to investigate
	KeepChars = Split("1 2 3 4 5 6 7 8 9 0", " ")
	' determine the text fragments that remain after removing all numbers
	DeleteTxt = Txt
	Forall C In KeepChars
		DeleteTxt = Cstr(Join(Split(DeleteTxt, C), ""))
	End Forall
	DeleteTxt = Split(DeleteTxt, " ")
	' now remove those fragments from the original text
	NumTxt = Txt
	Forall C In DeleteTxt
		NumTxt = Cstr(Join(Split(NumTxt, C), ""))
	End Forall
	NumTxt = Split(NumTxt, " ")
	' numbers remain; filter by size
	Forall C In NumTxt
		If Len(C)<>5 Then C=""
	End Forall
	Call UIDoc.FieldSetText("Zip", Fulltrim(Cstr(Join(NumTxt, " "))))
End Sub

Open in new window

LVL 46

Accepted Solution

Sjef Bosman earned 2000 total points
ID: 20854960
The silly way:

Dim s As String
Dim i As Integer

If field Like "*#####*" Then
      For i= Len(field)-4 To 1 Step -1 ' start at the back
            s= Mid$(field, i, 5)
            If s Like "#####" Then
                  ' found it!!
            End If
      ' no zip
End If

Not tested, not guaranteed...
LVL 46

Expert Comment

by:Sjef Bosman
ID: 20854969
Hey CRAK, working overtime eh? :-))
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

LVL 13

Expert Comment

ID: 20856860
No.... probably the same reasons as you: Nothing on TV / in for a challenge.
Tried a crash course Italian earlier on. I was able to read the middle part pretty well.... quite similar to BASIC really! ;-))

Author Closing Comment

ID: 31429335
Thanks to both of you for your help.  Either of these solutions will work well.  I went with Sjef's only because it involved less code.
LVL 31

Expert Comment

ID: 20866956

I've tried to do things somewhat similar to you, but inverse. You're actually doing a lot of processing to avoid brute force. On a short field (and addresses tend to be short fields!), brute force is actually more efficient.

However, something like this CAN be more efficient. Instead of dropping the numbers out, you drop everything else out, replacing it with spaces, triming extra spaces, and taking the last remaining string. I think I implemented that once, but can't find the code.
LVL 46

Expert Comment

by:Sjef Bosman
ID: 20868203
Heh, thought of that too, even in Formula it should be doable, but in LotusScript the complexity of the algorithm cannot really be justified. My other idea was to use a Regular Expression (there's a Windows DLL), but in this case that's really overkill. I assumed the Like-operator and the single loop would be the most effective way. Last one, and a totally absurd suggestion: generate an LR(1) parser using Yacc...

Thanks for the grade!!
LVL 13

Expert Comment

ID: 20870319
Most brute-force approaches I've seen continuously shorten source strings: they check the first 'n' characters, drop the 1st char and retry.
Approaches like mine may easily beat that, provided that the source string is large enough and there aren't too many occurrances of the searchstrings.
Sjef's code doesn't affect the source string at all! It doesn't waste time copying chunks of string up and down through RAM. No doubt it's faster.
Just please don't spoil my creative mood....

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

I thought it will be a good idea to make a post as it will help in case someone else faces these issues. I trust this gives an idea how each entry in Notes.ini can mean a lot for the Domino Server to be functioning properly. This article discusses t…
Article by: Rob
Notes 8.5 Archiving Steps and Tips This article covers setting up a Notes archive, and helps understand some of the menu choices making setting up and maintaining a Notes archive file easier.
Free Data Recovery software is an advanced solution from Kernel Tools to recover data and files such as documents, emails, database, media and pictures, etc. It supports recovery from physical & logical drive after a hard disk crash, accidental/inte…
Watch the video to know how one can repair corrupt Exchange OST file effortlessly and convert OST emails to MS Outlook PST file format by using Kernel for OST to PST converter tool. It can convert OST to MSG, MBOX, EML to access them. It can migrate…

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question