%% Extract ZIP Codes from Addresses
% Create a string array that contains addresses. Each address ends with a US ZIP code.

str = ["73 Beacon St., Boston, MA, 02116";
       "1640 Riverside Dr., Hill Valley, CA, 92530";
       "138 Main St., Cambridge, MA, 02138"]

%
% Create a pattern that matches any sequence of digits.

pat = digitsPattern

%
% Use it to extract all sequences of digits from the addresses.

newStr = extract(str,pat)

%
% The digitsPattern pattern matches street numbers, apartment numbers, and ZIP codes. To match only ZIP codes, create a pattern that matches a sequence of digits at the end of an address.

pat = digitsPattern + textBoundary

%
% Extract the ZIP codes.

newStr = extract(str,pat)

% For a list of functions that create pattern objects, see pattern.
% Copyright 2020 The MathWorks, Inc.