Quote
<a>sample link</a>
<a rel="" href="http://dwite.ca/">link with rel</a>
<a href="http://compsci.ca/" rel="nofollow">link with no follow</a>
<a href="http://compsci.ca/blog" rel="external">more rels</a>
<a href="http://compsci.ca/v3/viewforum.php?f=131" title="">link</a>
<a rel="" href="http://dwite.ca/">link with rel</a>
<a href="http://compsci.ca/" rel="nofollow">link with no follow</a>
<a href="http://compsci.ca/blog" rel="external">more rels</a>
<a href="http://compsci.ca/v3/viewforum.php?f=131" title="">link</a>
I'm trying to extract from the String the substring rel="" and anything that could possibly be in the quotes inside rel.
My current code is:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package dwite;
import java.io.*;
import java.util.*;
import java.util.regex.*;
/**
*
* @author brocj1112
*/
public class links {
public static void main(String[] args) throws IOException {
//Code goes here
Scanner fin = new Scanner(new FileReader("IN/links.in"));
String sHTML;
String sLink;
String sRel = "";
while (fin.hasNextLine()) {
sHTML = fin.nextLine();
// get just the link part
sLink = sHTML.substring(sHTML.indexOf("<a"),sHTML.indexOf("</a>")+4);
System.out.println(sLink);
if (!sLink.contains("rel=")) {
// rel is not found
// so add it to the end of the link
sLink = sLink.substring(0,sLink.indexOf(">")) + " rel=\"nofollow\">"
+ sLink.substring(sLink.indexOf(">")+1);
} else if (sLink.contains("rel=\"\"")) {
sLink = sLink.replace(" rel=\"\"", "");
sLink = sLink.substring(0,sLink.indexOf(">")) + " rel=\"nofollow\">"
+ sLink.substring(sLink.indexOf(">")+1);
} else if (!sLink.contains("nofollow")) {
// rel already exists so add nofollow to the end of the rel
sRel = sLink.substring(sLink.indexOf("rel=\""),sLink.indexOf("rel=\"", sLink.indexOf("rel=\"")));
}
}
fin.close();
}
}
The code that is trying to get the rel="" portion is:
sLink.substring(sLink.indexOf("rel=\""),sLink.indexOf("rel=\"", sLink.indexOf("rel=\"")));
However this code only returns:
rel=
and nothing else. Any ideas as to how to accomplish this?


Sign In
Create Account


Back to top










