📄 e441. using a regular expression to filter lines from a reader.txt
字号:
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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -