Link to home
Start Free TrialLog in
Avatar of KDrago
KDrago

asked on

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.
Avatar of CRAK
CRAK
Flag of Netherlands image

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

ASKER CERTIFIED SOLUTION
Avatar of Sjef Bosman
Sjef Bosman
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hey CRAK, working overtime eh? :-))
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! ;-))
Avatar of KDrago
KDrago

ASKER

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.
CRAK,

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.
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....
;-)