e441. using a regular expression to filter lines from a reader.txt

来自「这里面包含了一百多个JAVA源文件」· 文本 代码 · 共 23 行

TXT
23
字号
A common use of regular expressions is to find all lines that match a pattern, similar to the grep Unix command. This example reads lines using a BufferedReader and tests each line for a match. 
    try {
        // Create the reader
        String filename = "infile.txt";
        String patternStr = "pattern";
        BufferedReader rd = new BufferedReader(new FileReader(filename));
    
        // Create the pattern
        Pattern pattern = Pattern.compile(patternStr);
        Matcher matcher = pattern.matcher("");
    
        // Retrieve all lines that match pattern
        String line = null;
        while ((line = rd.readLine()) != null) {
            matcher.reset(line);
            if (matcher.find()) {
                // line matches the pattern
            }
        }
    } catch (IOException e) {
    }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?