Search address string and parse out zip code

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.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Sjef BosmanGroupware ConsultantCommented:
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...

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Sjef BosmanGroupware ConsultantCommented:
Hey CRAK, working overtime eh? :-))
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

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! ;-))
KDragoAuthor Commented:
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.

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.
Sjef BosmanGroupware ConsultantCommented:
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!!
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....
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Lotus IBM

From novice to tech pro — start learning today.