views:

62

answers:

3

i want to know how to get the string from group of string

 String j = "<a ............> hai</a>";

i want to get the string of tag of

  <a ...> ... </a>

thanks and advance

+2  A: 

I'd use an XML parser for that and extract the attributes in question.

Bozho
+1  A: 

I agree with Bozho. If you need to do this on a regular basis, an XML or HTML Parser would be much less error prone.

For a quick and dirty approach, you can use the Regex

(href|src)="[^"]*"

Make sure to escape all these quotes when trying that.

Jens
A: 

I hate regex, I would have done it this way..

str = str.substring(str.indexOf("href="));
str = str.substring(0, str.indexOf("\"", 5);  //'href=' is 5 chars 
//str = str.substring(0, str.indexOf(" ");  //this is more readable I think

Do the same for src=

Note, you will get href= as part of the string as well, how to prevent that is left as an exercise. lol. (HINT: change 0 in the second line to ...)

Rosdi
In this case `str` will contain just `href=`, probably not what he wants.
Christoffer Hammarström
You are right, duh!.. I fixed it.. (I think), didn't actually test it.
Rosdi