i'm having trouble coming regex capable of validating postcode (10,000 actually) can read entries in there current form. program using validator uk postcode validator created. stuck on , having trouble on figuring out how proceed.
package postcodesort; import java.util.regex.matcher; import java.util.regex.pattern; /** * * */ public class zipcodevalidator { private static string regex = "^[a-z]{1,2}[0-9r][0-9a-z]? [0-9][abd-hjlnp-uw-z]{2}$"; private static pattern pattern = pattern.compile(regex, pattern.case_insensitive); public boolean isvalid(string zipcode) { matcher matcher = pattern.matcher(zipcode); return matcher.matches(); } }
below small example of data in text file.
"01","35005","al","adamsville",86.959727,33.588437,10616,0.002627
"05","72001","ar","adona",92.903325,35.046956,494,0.00021
"06","90804","ca","signal hill",118.155187,33.782993,36092,0.001213
so want read first 3 sets of data. "01","35006","al" read , validated whilst rest ignored. long has 2 numbers, 5 numbers , 2 letters validate postcode. don't know how make happen.
any , appreciated!
description
^"([0-9]{2})","([0-9]{5})","([a-z]{2})"
this regular expression following:
- reads first 3 sets of data.
- validates first value has 2 numbers
- validates second value has 5 numbers
- validates third value has 2 letters
- captures individual values
example
live demo
https://regex101.com/r/bo7qk7/1
sample text
"01","35005","al","adamsville",86.959727,33.588437,10616,0.002627 "05","72001","ar","adona",92.903325,35.046956,494,0.00021 "06","90804","ca","signal hill",118.155187,33.782993,36092,0.001213
sample matches
- capture group 0 gets entire string first 3 values
- capture group 1 gets value inside quotes first value
- capture group 2 gets value inside quotes first value
- capture group 3 gets value inside quotes first value
[0][0] = "01","35005","al" [0][1] = 01 [0][2] = 35005 [0][3] = al [1][0] = "05","72001","ar" [1][1] = 05 [1][2] = 72001 [1][3] = ar [2][0] = "06","90804","ca" [2][1] = 06 [2][2] = 90804 [2][3] = ca
explanation
node explanation ---------------------------------------------------------------------- ^ beginning of "line" ---------------------------------------------------------------------- " '"' ---------------------------------------------------------------------- ( group , capture \1: ---------------------------------------------------------------------- [0-9]{2} character of: '0' '9' (2 times) ---------------------------------------------------------------------- ) end of \1 ---------------------------------------------------------------------- "," '","' ---------------------------------------------------------------------- ( group , capture \2: ---------------------------------------------------------------------- [0-9]{5} character of: '0' '9' (5 times) ---------------------------------------------------------------------- ) end of \2 ---------------------------------------------------------------------- "," '","' ---------------------------------------------------------------------- ( group , capture \3: ---------------------------------------------------------------------- [a-z]{2} character of: 'a' 'z' (2 times) ---------------------------------------------------------------------- ) end of \3 ---------------------------------------------------------------------- " '"' ----------------------------------------------------------------------