java - 用于验证文件的正则表达式或函数?

标签 java regex

我手头上有一项任务是验证包含以下数据的文本文件的架构

50 个条目,格式如下,

序列号为 1-50,后跟一个制表符,后跟一个范围为 100<=n<=500 的随机数 n

e.g. 1 <tab> 256

由于正则表达式更容易检查文件的架构并且更易于维护,因此我更喜欢使用正则表达式而不是使用解析每个字符串并立即验证的类

输出文件应该类似于

Line 1 formatted correctly
Invalid format on line 2 (51 1000) + (Error message that can be set using a custom exception class)

我的问题是,正则表达式是否足够强大,可以为我提供所需的输出,即引发异常以正确的方式设置?

我的尝试如下

public class TestOutput {

    private final int MAX_LINES_TO_READ = 50;

    private final String REGEX = "RAWREGEX";

    public void testFile(String fileName) {

        int lineCounter = 1;

        try {

            BufferedReader br = new BufferedReader(new FileReader(fileName));

            String line = br.readLine();

            while ((line != null) && (lineCounter <= MAX_LINES_TO_READ)) {

                // Validate the line is formatted correctly based on regular expressions                
                if (line.matches(REGEX)) {
                    System.out.println("Line " + lineCounter + " formatted correctly");
                }
                else {
                    System.out.println("Invalid format on line " + lineCounter + " (" + line + ")");
                }

                line = br.readLine();
                lineCounter++;
            }

            br.close();

        } catch (Exception ex) {
            System.out.println("Exception occurred: " + ex.toString());
        }
    }

    public static void main(String args[]) {

        TestOutput vtf = new TestOutput();

        vtf.testFile("transactions.txt");
    }   
}

这是我的问题

  1. 最佳设计应该是什么样子(是否使用正则表达式)?
  2. 如果是,使用什么正则表达式?

最佳答案

使用这个正则表达式:

String REGEX = "([1-9]|[1-4]\\d|50)\t([1-4]\\d\\d|500)";

参见live demo .

解释一下...

[1-9]|[1-4]\\d|50 表示“1-50 中的任何数字”,通过 1-9、10-49 和 50 三种交替实现。

同样,[1-4]\\d\\d|500表示“100-500”,通过两次交替100-499和500实现。

只有 50 行,“性能”就无关紧要了(除非你每秒执行 100 次)——选择最易读和最容易理解的方法。如果您可以使用正则表达式,通常会减少代码,并且性能足够好。


测试代码:

private final String REGEX = "([1-9]|[1-4]\\d|50)\\t([1-4]\\d\\d|500)";

public void testFile(String fileName) {
    int lineCounter = 1;
    try {
        BufferedReader br = new BufferedReader(new FileReader(fileName));
        String line = br.readLine();
        while ((line != null) && (lineCounter <= MAX_LINES_TO_READ)) {
            if (line.matches(REGEX)) {
                System.out.println("Line " + lineCounter + " formatted correctly");
            } else {
                System.out.println("Invalid format on line " + lineCounter + " (" + line + ")");
            }
            line = br.readLine();
            lineCounter++;
        }
        br.close();
    } catch (Exception ex) {
        System.out.println("Exception occurred: " + ex.toString());
    }
}

测试文件:

1   123
50  346
23  145
68  455
1   535

输出:

Line 1 formatted correctly
Line 2 formatted correctly
Line 3 formatted correctly
Invalid format on line 4 (68    455)
Invalid format on line 5 (1 535)

关于java - 用于验证文件的正则表达式或函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57256016/

相关文章:

java - 注册期间用于密码检查的简单正则表达式是什么?

java - RegExp - 替换精确的字符串

java - 聊天应用程序的 "Send"按钮不起作用

java - 打开 SubDialog 后,InputMap/ActionMap 不起作用

java - JUnit Eclipse - 成功时显示堆栈跟踪

php正则表达式获取href标签内的字符串

mysql - 转换器 Perl 脚本修复

java - 如何以最低要求运行 Spring Boot 应用程序?

java - 弱要求 : how to make sure an object is initialized

python - 正则表达式提取嵌套模式