/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
{
String source
= "\\begin\\{theorem\\} Hello, World! \\end\\{theorem\\}"; LatexTheoremProofExtractor extractor = new LatexTheoremProofExtractor(source);
extractor.parse();
ArrayList<String> theorems = extractor.getTheorems();
System.
out.
println(theorems.
get(0).
trim()); System.
out.
println("Hello, World!"); }
}
class LatexTheoremProofExtractor {
// This is the LaTeX source that will be processed
// These are the list of theorems and proofs that are extracted, respectively
private ArrayList<String> theorems = null;
private ArrayList<String> proofs = null;
// These are the patterns to match theorems and proofs, respectively
private static final Pattern THEOREM_REGEX = Pattern.compile(Pattern.quote("\\begin\\{theorem\\}") + "(.+?)" + Pattern.quote("\\end\\{theorem\\}"));
private static final Pattern PROOF_REGEX = Pattern.compile(Pattern.quote("\\begin\\{proof\\}") + "(.+?)" + Pattern.quote("\\end\\{proof\\}"));
LatexTheoremProofExtractor
(String source
) { this.source = source;
}
public void parse() {
extractEntity("theorem");
extractEntity("proof");
}
private void extractTheorems() {
if(theorems != null) {
return;
}
theorems = new ArrayList<String>();
final Matcher matcher = THEOREM_REGEX.matcher(source);
while (matcher.find()) {
theorems.
add(new String(matcher.
group(1))); }
}
private void extractProofs() {
if(proofs != null) {
return;
}
proofs = new ArrayList<String>();
final Matcher matcher = PROOF_REGEX.matcher(source);
while (matcher.find()) {
proofs.
add(new String(matcher.
group(1))); }
}
private void extractEntity
(final String entity
) { if(entity.equals("theorem")) {
extractTheorems();
} else if(entity.equals("proof")) {
extractProofs();
} else {
// TODO: Throw an exception or something
}
}
public ArrayList<String> getTheorems() {
return theorems;
}
}