regex
Match address using regular expressions
This is an example of how to match an address using regular expressions. We have created a method boolean isValidAddr(String addr)
and used it to match some addresses to a specified pattern. The method is described below:
- The method reads a String address and returns true if a specified pattern matches the address and false otherwise.
- It has a a String
nameToken
, that is a regular expression consisting of an uppercase character followed by a capturing group of a lowercase character and a non-whitespace character once or not at all. - The
namePattern
is also a String regular expression consisting of thenameToken
at least 2 but no more that 3 times. - The
zipCodePattern
is also a String regular expression consisting of a digit appearing exactly 5 times and a capturing group that consists of the – followed by a digit exactly 4 times. - The
addressPattern
that is the full regular expression to match the input address consists of the ^ to match the beginning of a line, then thenamePattern
, then a word character one or more times, any character as many times, again a word character one or more times, thezipCodePattern
and then the $ to match a line ending. - The input String is matched to the constructed regular expression, using
matches(String regex)
API method of String and the result is returned.
Let’s take a look at the code snippet that follows:
package com.javacodegeeks.snippets.core; public class MatchAddress { public static void main(String args[]) { isValidAddr("John Smith 888 Luck Street,NY 64332"); isValidAddr("John A. Smith 888 Luck Street, NY 64332-4453"); isValidAddr("John Allen Smith 888 Luck Street, NY 64332-4453"); isValidAddr("888 Luck Street, NY 64332"); isValidAddr("P.O. BOX 888 Luck Street, NY 64332-4453"); isValidAddr("John Allen Smith 888 Luck st., NY"); } public static boolean isValidAddr(String addr) { boolean retval = false; String nameToken = "\p{Upper}(\p{Lower}+\s?)"; String namePattern = "(" + nameToken + "){2,3}"; String zipCodePattern = "\d{5}(-\d{4})?"; String addressPattern = "^" + namePattern + "\w+ .*, \w+ " + zipCodePattern + "$"; retval = addr.matches(addressPattern); String msg = "NO MATCHnpattern:n " + addr + "nregexLength:n " + addressPattern; if (retval) { msg = "MATCHnpattern:n " + addr + "nregexLength:n " + addressPattern; } System.out.println(msg + "rn"); return retval; } }
Output:
NO MATCH
pattern:
John Smith 888 Luck Street,NY 64332
regexLength:
^(p{Upper}(p{Lower}+s?)){2,3}w+ .*, w+ d{5}(-d{4})?$
NO MATCH
pattern:
John A. Smith 888 Luck Street, NY 64332-4453
regexLength:
^(p{Upper}(p{Lower}+s?)){2,3}w+ .*, w+ d{5}(-d{4})?$
MATCH
pattern:
John Allen Smith 888 Luck Street, NY 64332-4453
regexLength:
^(p{Upper}(p{Lower}+s?)){2,3}w+ .*, w+ d{5}(-d{4})?$
NO MATCH
pattern:
888 Luck Street, NY 64332
regexLength:
^(p{Upper}(p{Lower}+s?)){2,3}w+ .*, w+ d{5}(-d{4})?$
NO MATCH
pattern:
P.O. BOX 888 Luck Street, NY 64332-4453
regexLength:
^(p{Upper}(p{Lower}+s?)){2,3}w+ .*, w+ d{5}(-d{4})?$
NO MATCH
pattern:
John Allen Smith 888 Luck st., NY
regexLength:
^(p{Upper}(p{Lower}+s?)){2,3}w+ .*, w+ d{5}(-d{4})?$
This was an example of how to match an address using regular expressions in Java.