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:
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | 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.