Java - 用于验证文本文件中的美国邮政编码的正则表达式

时间:2016-06-06 19:07:42

标签: java regex netbeans

我无法想出能够验证美国邮政编码(实际上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个数字和两个字母,那么它将是一个验证邮政编码。我不知道如何实现这一目标。

感谢任何和所有帮助!

3 个答案:

答案 0 :(得分:0)

如果您只传递5位数的邮政编码,可以通过将正则表达式修改为以下内容来验证它:

^[0-9]{5}$

如果有用户使用ZIP + 4格式,例如:12345-3333,请使用以下正则表达式验证:

^[0-9]{5}(?:-[0-9]{4})?$

但是,如果您想根据提供的邮政编码验证城市名称,您可能需要查看讨论here

答案 1 :(得分:0)

描述

implicit

Regular expression visualization

此正则表达式将执行以下操作:

  • 返回看似位于第二个引用位置的邮政编码
  • 对于这个特定问题,^(?:"[^"]*",){1}"([^"]*)" 可能有点过分,但是该构造允许您在返回实际值之前准确指定要跳过多少引号逗号分隔值。在这种情况下,我们正在跳过(?:"[^"]*",){1}字段
  • 返回不包含引号的值

实施例

现场演示

https://regex101.com/r/cJ6iE9/1

示例文字

1

样本匹配

  • 捕获组0将整个字符串移至相关字段
  • Capture group 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})"

Regular expression visualization

此正则表达式将执行以下操作:

  • 读取前三组数据。
  • 验证第一个值有两个数字
  • 验证第二个值有5个数字
  • 验证第三个值有两个字母
  • 捕获各个值

实施例

现场演示

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获取前三个值的整个字符串
  • 捕获组1获取第一个值的引号内的值
  • 捕获组2获取第一个值的引号内的值
  • 捕获组3获取第一个值的引号内的值
[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
----------------------------------------------------------------------
  "                        '"'
----------------------------------------------------------------------