You are misunderstanding the purpose of Matcher
's find()
and replaceAll()
functions. It makes no sense to use replaceAll()
inside a find()
loop:
while (pochhammer.find()){
System.out.println(line);
String line2=pochhammer.replaceAll("\\\\pochhammer{" + pochhammer.group(2) + "}{" + pochhammer.group(3) + "}");
System.out.println(line2);
}
To replace all instances, you'll need to change it to something like this:
StringBuffer rplcmntBfr = new StringBuffer();
while(pochhammer.find()) {
pochhammer.appendReplacement(rplcmntBfr, "\\\\pochhammer{" + pochhammer.group(2) + "}{" + pochhammer.group(3) + "}");
}
pochhammer.appendTail(rplcmntBfr);
System.out.println(rplcmntBfr);
Accessing individual match-groups and using replaceAll(s)
pochhammer.replaceAll("\\\\pochhammer{" + pochhammer.group(2) + "}{" + pochhammer.group(3) + "}");
makes no sense, because group(i)
is only for use with find()
, yet replaceAll(s)
is for replacing all matches in the line at once.
Here's a tutorial on making replacements with find()
, appendReplacement(sb,s)
and appendTail(sb)
: http://tutorials.jenkov.com/java-regex/matcher.html#appendreplacement-appendtail-methods
You may also want to take a look at this question, regarding the difference between Greedy vs. Reluctant vs. Possessive Quantifiers, as I suspect that the quantifiers in your regex may need to change from +
, to either +?
or ++
. But since I don't know what your input or expected output looks like, I can only guess at this part.
The problem I mention above is the big one.
Good luck.