开发者

Get particular string using regex java

I want to know how to get the string out of a group of strings

String j = "<a ..........开发者_JAVA百科..> hai</a>";

I want to get the string for the tag

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


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


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.


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 ...)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜