【发布时间】:2017-06-24 14:20:54
【问题描述】:
我想在日志文件中查找由正则表达式模式定义的警告 (是的 tex 日志文件) 并在 tex 文件中找到模式,这表示 它是一个主文件。
为此,我逐行读取文件并匹配模式。 只要图案只有一条线,它就可以正常工作。
// may throw FileNotFoundException < IOExcption
FileReader fileReader = new FileReader(file);
// BufferedReader for perfromance
BufferedReader bufferedReader = new BufferedReader(fileReader);
Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);//
// readLine may throw IOException
for (String line = bufferedReader.readLine();
line != null;
// readLine may thr. IOException
line = bufferedReader.readLine()) {
if (pattern.matcher(line).find()) {
return true;
}
}
return false;
如果它跨行传播,这种方法就会变得困难。
我试过了
CharBuffer chars = CharBuffer.allocate(1000);
// may throw IOException
int numRead = bufferedReader.read(chars);
System.out.println("file: "+file);
System.out.println("numRead: "+numRead);
System.out.println("chars: '"+chars+"'");
return pattern.matcher(chars).find();
但这不起作用:根本没有匹配! numRead 产生 1000 而 chars 似乎是 ''!!!!
示例:模式: \A(\RequirePackage\s*([(\s|\w|,)])?\s{\w+}\s*([(\d|.)+])? | \PassOptionsToPackage\s*{\w+}\s*{\w+}| %.$| \输入{[^{}]}| \s)* \(文档样式|文档类)
是我的乳胶主文件模式。 附上一份这样的文件:
\RequirePackage[l2tabu, orthodox]{nag}
\documentclass[10pt, a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage{fancyvrb}
\title{The dvi-format and the program dvitype}
\author{Ernst Reissner (rei3ner@arcor.de)}
\begin{document}
\maketitle
\tableofcontents
\section{Introduction}
This document describes the dvi file format
traditionally used by \LaTeX{}
and still in use with \texttt{htlatex} and that like.
如何解决这个问题?
【问题讨论】:
-
通过向我们展示 a) 您尝试匹配的正则表达式模式和 b) 应该匹配的日志文件样本来更新您的问题。
标签: java regex filereader