-1

I have a tag in xml file like

<a href="/abc/xyz/ccc" id="link_abc" title="bala" name="link_abc"></a>

so i am storing this value in a string called tempString; Now i have to parse the tempString so that i can extract only href attribute value. the output i am expecting is /abc/xyz/ccc. I am looking for Java regular expression for doing this.

I would really appreciate your help on this.

Regards, Ramakrishna.

srk
  • 3,606
  • 2
  • 18
  • 23
  • 1
    @DNA: first he'd need to make sure that the HTML was in proper XML form such as by becoming XHTML compliant. Or even easier, simply use an HTML parser. – Hovercraft Full Of Eels Sep 28 '12 at 22:12
  • XML is similar to HTML in the difficulty of parsing it using regular expressions, so http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – Mike Samuel Sep 28 '12 at 22:13
  • 1
    Argh, I'd resisted the urge to link to [THAT PAGE](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454).... – DNA Sep 28 '12 at 22:14
  • Let me give you back ground. While unmarshalling(JAXB) the Link property has below value. Link = ; Now i have to extract href attribute value and reassign it to Link property. May be the only option i can see is using Java Regular expression. I couln't use any other API. – srk Sep 28 '12 at 22:18
  • @user1635014 have you tried my approach ? – PermGenError Sep 28 '12 at 22:46

2 Answers2

1

Why not use jsoup ?

i.e :

Document doc = Jsoup.connect("http://yoururl.com/").get();
Elements link = doc.select("#link_abc");
String href = link.attr("href");
ant
  • 22,634
  • 36
  • 132
  • 182
0

you could use String.split();

    String h = "<a href=\"/abc/xyz/ccc\" id=\"link_abc\" title=\"bala\"   name=\"link_abc\">planx</a>";
System.out.println(h.split("href")[1].substring(2).split("\"")[0]);


OUTPUT: /abc/xyz/ccc
PermGenError
  • 45,977
  • 8
  • 87
  • 106