Created
May 22, 2016 09:27
-
-
Save rominf/7ab4cee09f386b2d9269de7f88716626 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
String normalizePostcode(String postcode, String country) { | |
Map<String, List<String>> rules = new TreeMap<String, List<String>>() {{ | |
put("Netherlands", Arrays.asList("(NL-)?(\\d{4})\\W*([A-Z]{2})", , "\$2\$3")); | |
put("United Kingdom", Arrays.asList("(?i)([A-Z]{1,2}[0-9]{1,2}[A-Z]?)\\s*([0-9][A-Z]{2})", "\$1\$2")); | |
}} | |
Pattern pattern = Pattern.compile(rules.get(country).get(0)); | |
String replacement = rules.get(country).get(1); | |
Matcher matcher = pattern.matcher(postcode); | |
return matcher.replaceAll(replacement); | |
} | |
System.out.println(); | |
System.out.println(normalizePostcode("1101 DL", "Netherlands")); | |
System.out.println(normalizePostcode("1101-DL", "Netherlands")); | |
System.out.println(normalizePostcode("b288qp", "United Kingdom")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment