In the United States, the Post Office assigns a ZIP code to each street address, in order to make sorting and delivery more efficient (ZIP stands for Zone Improvement Plan).
ZIP codes used to contain five digits. A few years ago, however, the Post Office added four optional digits -- a two-digit "sector" and a two-digit "segment" -- to provide finer granularity. Normally the full nine-digit ZIP code is shown as five digits, followed by a hyphen, followed by the four sector-segment digits.
At my company, a ZIP code appearing on a service order may be either five digits or nine digits, and there may be one or more blank spaces on either side of the hyphen.
This syntax is not hard to parse using more conventional techniques, but I chose to use a finite state machine based on the state transition diagram to the left.
To avoid cluttering the diagram, I left out the error paths. Any possible input for which no transition is specified leads to an error state.
This diagram is particularly easy to follow. Apart from error states
there are three terminal states, corresponding to a nine-digit ZIP, a
five-digit ZIP, or no ZIP. I encoded the states in a WORKING-STORAGE
table and stepped through them in a short loop.