我无法想出能够验证美国邮政编码(实际上10,000个)的正则表达式,该邮政编码能够以当前形式读取我的条目。我的程序正在使用我创建的英国邮政编码验证器的验证器。我非常坚持这一点,并且无法弄清楚如何继续进行。
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();
}
}
以下是我的文本文件中的一个小例子。
“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
所以我想让它读取前三组数据。因此,“01”,“35006”,“AL”将被读取和验证,而其余部分将被忽略。因此,只要它有两个数字,5个数字和两个字母,那么它将是一个验证邮政编码。我不知道如何实现这一目标。
感谢任何和所有帮助!
答案 0 :(得分:0)
如果您只传递5位数的邮政编码,可以通过将正则表达式修改为以下内容来验证它:
^[0-9]{5}$
如果有用户使用ZIP + 4格式,例如:12345-3333,请使用以下正则表达式验证:
^[0-9]{5}(?:-[0-9]{4})?$
但是,如果您想根据提供的邮政编码验证城市名称,您可能需要查看讨论here
答案 1 :(得分:0)
implicit
此正则表达式将执行以下操作:
^(?:"[^"]*",){1}"([^"]*)"
可能有点过分,但是该构造允许您在返回实际值之前准确指定要跳过多少引号逗号分隔值。在这种情况下,我们正在跳过(?:"[^"]*",){1}
字段现场演示
https://regex101.com/r/cJ6iE9/1
示例文字
1
样本匹配
"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
[0][0] = "01","35005"
[0][1] = 35005
[1][0] = "05","72001"
[1][1] = 72001
[2][0] = "06","90804"
[2][1] = 90804
答案 2 :(得分:0)
^"([0-9]{2})","([0-9]{5})","([a-z]{2})"
此正则表达式将执行以下操作:
现场演示
https://regex101.com/r/bO7qK7/1
示例文字
"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
样本匹配
[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
NODE EXPLANATION
----------------------------------------------------------------------
^ the beginning of a "line"
----------------------------------------------------------------------
" '"'
----------------------------------------------------------------------
( group and capture to \1:
----------------------------------------------------------------------
[0-9]{2} any character of: '0' to '9' (2 times)
----------------------------------------------------------------------
) end of \1
----------------------------------------------------------------------
"," '","'
----------------------------------------------------------------------
( group and capture to \2:
----------------------------------------------------------------------
[0-9]{5} any character of: '0' to '9' (5 times)
----------------------------------------------------------------------
) end of \2
----------------------------------------------------------------------
"," '","'
----------------------------------------------------------------------
( group and capture to \3:
----------------------------------------------------------------------
[a-z]{2} any character of: 'a' to 'z' (2 times)
----------------------------------------------------------------------
) end of \3
----------------------------------------------------------------------
" '"'
----------------------------------------------------------------------