Когда я запускаю этот код, он распечатывает только группы, найденные в первом шаблоне, который он соответствовал за строку. Тем не менее, есть несколько строк, которые я хочу заменить в каждой строке, и я хочу, чтобы она распечатала конкретные группы для каждой строки в схеме, который он соответствовал. Как я могу изменить его так, чтобы он распечатал группы, специфичные для шаблонов /струн, которые он обнаружил в каждой строке вместо печати только группы в первом шаблоне, которые они найдены? < /P>
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.io.FileNotFoundException;
import java.io.File;
public class RealReadFile {
private static final String fileName = "KLSadd.tex";
private Scanner myFile = null;
// No-args constructor, create a new scanner for the specific file defined
// above
public RealReadFile() throws FileNotFoundException {
if (myFile == null)
myFile = new Scanner(new File(fileName));
}
// One-arg constructor - the name of the file to open
public RealReadFile(String name) throws FileNotFoundException {
if (myFile != null)
myFile.close();
myFile = new Scanner(new File(name));
}
public boolean endOfFile() { // Return true is there is no more input
return !myFile.hasNext(); // hasNext() returns true if there is more input, so I negate it
}
public String nextLine() {
return myFile.nextLine().trim();
}
public static void main(String[] args) throws FileNotFoundException {
RealReadFile file = new RealReadFile();
while(!file.endOfFile()) {
String line = file.nextLine();
Pattern cpochhammer = Pattern.compile("(\\(([^\\)]+)\\)_\\{?([^\\}]+)\\}?)");
Matcher pochhammer = cpochhammer.matcher(line);
while (pochhammer.find()){
System.out.println(line);
String line2=pochhammer.replaceAll("\\\\pochhammer{" + pochhammer.group(2) + "}{" + pochhammer.group(3) + "}");
System.out.println(line2);
}
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/221 ... ith-groups