验证LaTeX文件,java
所以我有这个文本文件验证LaTeX文件,java,java,string,split,organization,Java,String,Split,Organization,所以我有这个文本文件 \begin{document} {\Large \begin{center} Homework Problems \end{center}}\begin{itemize}\item\end{itemize} \begin{enumerate} \begin{proof} \begin{align} \en
\begin{document}
{\Large \begin{center} Homework Problems \end{center}}\begin{itemize}\item\end{itemize}
\begin{enumerate}
\begin{proof}
\begin{align}
\end{align}
\end{proof}
\begin{proof}
\begin{align}
\end{align}
\end{proof}
\end{enumerate}
\end{document}
我想遍历每一行,找到所有的“\begin”片段,然后在“{{}”中取字符串并将其存储在堆栈中。当找到相应的“\end”时,我调用堆栈上的pop()命令并将其删除。不过我有一些问题
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.Stack;
import java.util.StringTokenizer;
public class LaTeXParser{
public static void main(String args[]) throws FileNotFoundException{
Scanner scan = new Scanner(System.in);
Stack s = new Stack();
int lineCount = 0;
String line;
String nextData = null;
String title = null;
String fname;
System.out.print("Enter the name of the file (no extension): ");
fname = scan.next();
fname = fname + ".txt";
FileInputStream fstream = new FileInputStream(fname);
Scanner fscan = new Scanner(fstream);
System.out.println();
while(fscan.hasNextLine()){
lineCount++;
line = fscan.nextLine();
StringTokenizer tok = new StringTokenizer(line);
while(tok.hasMoreElements()){
nextData = tok.nextToken();
System.out.println("The line: "+nextData);
if(nextData.contains("\\begin") && !nextData.contains("\\end")){
if(nextData.charAt(1) == 'b'){
title = nextData.substring(nextData.indexOf("{") + 1, nextData.indexOf("}"));
s.push(title);
}
else{
//title = nextData.substring();
}
}//end of BEGIN if
if(nextData.contains("\\end") && !nextData.contains("\\begin")){
if(s.peek().equals(nextData.substring(nextData.indexOf("{") + 1, nextData.indexOf("}")))){
s.pop();
}
}//end of END if
if(nextData.contains("\\begin") && nextData.contains("\\end")){
String[] theLine = nextData.split("[{}]");
for(int i = 0 ; i < theLine.length ; i++){
if(theLine[i].equals("\\end") && theLine[i+1].equals(s.peek())){
s.pop();
}
if(theLine[i].equals("\\begin")){
title = theLine[i+1];
s.push(title);
}
}
}//end of BEGIN AND END if
}
}//end of whiles
fscan.close();
while(!s.isEmpty()){
System.out.println("the top "+s.pop());
}
}
}
请看,我可以进入“\begin”并添加适当的字符串,但它只是移动并传递“\end{itemize}”。怎么解决这个问题
实际上,即使在按下“itemize”字符串之后,它也应该检查并正常执行,但它不工作!我相信这与“\end”有关,有人能证实吗?它跳过了这一步,显然是因为它不适合条件,但它适用于其他线路。只是不是这个特殊的案例 您可能需要避开反斜杠,因此请编写
\\
而不是\
。如果它们在正则表达式(regexpr)中,则需要对它们进行两次转义:\\\
;我认为不需要括号。你说的“regexprs”是什么意思?我怎么知道什么时候有东西在里面?你有没有使用调试器一步一步地进入你的代码?没有,我一直在使用调试方法来测试它,printlns,等等。
\begin{itemize}\item\end{itemize}