【发布时间】:2014-08-22 18:43:54
【问题描述】:
我有一个需要验证的文件。该行可以是以下格式之一:
<String>:<Number>:<String>:<String> <Number>
<String>:<Number>:<String> <Number>
<String>:<Number> <Number>
<String> <Number>
我打算使用多个正则表达式来匹配该行。这是一个好方法吗?
while ((line = file.readLine()) != null) {
boolean match = false;
if (line.matches(pattern1)) {
match = true;
}
if (line.matches(pattern2)) {
match = true;
}
if (line.matches(pattern3)) {
match = true;
}
if (line.matches(pattern4)) {
match = true;
}
if (!match) {
throw new RuntimeException("Invalid format")
}
}
感谢您的反馈。谢谢。
【问题讨论】:
标签: java regex validation