Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用于在文件中进行匹配的java正则表达式_Java_Regex_Filereader - Fatal编程技术网

用于在文件中进行匹配的java正则表达式

用于在文件中进行匹配的java正则表达式,java,regex,filereader,Java,Regex,Filereader,我想在日志文件中查找由正则表达式模式定义的警告 (是tex日志文件) 还可以在tex文件中找到表示 它是一个主文件 为此,我逐行读取文件并匹配模式。 只要图案只有一条线,这就可以正常工作 // may throw FileNotFoundException < IOExcption FileReader fileReader = new FileReader(file); // BufferedReader for perfromance BufferedReader buffered

我想在日志文件中查找由正则表达式模式定义的警告 (是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;
但这不起作用:根本没有匹配!! numRead产生1000个字符,而chars似乎是“”

示例:模式: \(\RequirePackage\s*([(\s |\w |,)])?\s{\w+}\s*([(\d |])+])| \PassOptionsToPackage\s*{\w+}\s*{\w+}| %.$| \输入{[^{}]}| \(s)* \(documentstyle | documentclass)

是latex主文件的模式。 其中一份文件附在以下部分:

\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. 

如何解决这个问题

如果需要多行匹配,并且日志文件不太大,则可以在一个字符串中读取整个文件:

String content = new Scanner(file).useDelimiter("\\Z").next();

然后针对
内容运行regex

更新您的问题,方法是向我们显示a)您尝试匹配的regex模式和b)应该匹配的日志文件示例。
String content = new Scanner(file).useDelimiter("\\Z").next();