开发者

html search and replace on server side

I like to search something like stack <"sometag"> overflow</"sometag"> and replace with stack <"sometag">underflow</"sometag">

It is part of a large html text and I like to do it in Java (there is some limitation on the server side technologies that I can use). I searched through and found this post: How to find/replace text in html while preserving html tags/structure

One of the answers there suggests marking with special markers, producing plain text and then using regex. Finally unmarking and getting back the string to html. But it assumes that the string comes at a given position in the text. I have no way of knowing where the strings will be and how many times it may repeat.

Of course direct usage of regular expression search and replace is not appropriate here since I need to prese开发者_如何转开发rve the html tags.

Thanks in advance!


I would be somewhat wary of doing regexps to change an HTML file. Too many things would go wrong.

Are your HTML files XML compliant? (e.g., XHTML?). In that case, you might be better off doing XML level transformations, with either XSL or a query based parser.


Take look on http://jsoup.org/ It does all kinds of stuff with tags.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜