I was wondering if there is a 'proper' way in Java to extract a sub-string (from a string) that matches a particular pattern.
I want to extract a postal code from a full address string.
Postal code is A1A 1A1 format (where 1 is numeric between 0 and 9 and A is alphabetical A to Z).
The address example could be "123 Main Street, Toronto, Ontario, Canada, M3N 5H6" or "123 Main Street, Toronto, Ontario, Canada, 23456" (US zip code).
I would need to be able to extract M3N 5H6
Is there a way in Java to do some pattern matching in order to extract the postal code (if it exists) from the string? The assumption is that it could appear anywhere in the string.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
David, only Canadian postal codes, sorry I should've been more clear.
James, yes the postal code would be the same format, but it could appear anywhere in the string (meaning the address string is not always consistent format)