I've a file which contains self closing anchor tags
<p><a name="impact"/><span class="sectiontitle">Impact</span></p>
<p><a name="Summary"/><span class="sectiontitle">Summary</span></p>
i want to correct the tags like below
<p><a name="impact"><span class="sectiontitle">Impact</span></a></p>
<p><a name="Summary"><span class="sectiontitle">Summary</span></a></p>
I've written this code to find and replace incorrect anchor tags
package mypack;
import java.io.*;
import java.util.regex.*;
public class AnchorIssue {
static int count=0;
public static void main(String[] args) throws IOException {
Pattern pFinder = Pattern.compile("<a name=\\\".*\\\"(\\/)>(.*)(<)");
BufferedReader r = new BufferedReader
(new FileReader("D:/file.txt"));
String line;
while ((line =r.readLine()) != null) {
Matcher m1= pFinder.matcher(line);
while (m1.find()) {
int start = m1.start(0);
int end = m1.end(0);
++count;
// Use CharacterIterator.substring(offset, end);
String actual=line.substring(start, end);
System.out.println(count+"."+"Actual String :-"+actual);
actual.replace(m1.group(1),"");
System.out.println(actual);
actual.replaceAll(m1.group(3),"</a><");
System.out.println(actual);
// Use CharacterIterator.substring(offset, end);
System.out.println(count+"."+"Replaced"+actual);
}
}
r.close();
}
}
The above code returns the correct number of self-closing anchor tags in file but the replace code is not working properly.