user576249
user576249

Reputation: 31

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 preserve the html tags.

Thanks in advance!

Upvotes: 1

Views: 482

Answers (2)

MatBanik
MatBanik

Reputation: 26860

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

Upvotes: 1

Uri
Uri

Reputation: 89749

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.

Upvotes: 1

Related Questions