Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

How to replace a string in a HTML string

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I have HTML string, for instance



There could be some more tags present and also the string could be anything.

Now i have to write a java code to replace the string(Testingfg in this case) with another string like Testing.


One way to solve this is by using pattern match and then replace. Please let me know how to write a regular expression in this case or if there is any way to do this.

Thanks in advance,
Asha


 
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why cant this be done with a simple String replace method? Read the whole html text into a String and call replace method on it

 
asha ganapathy
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is to traverse through the HTML string and extract only the string, this extracted string would be later replaced by another one.
The string is entered by the user and this could be even in this way.




Please let me know if there any way to extract only the string.

Thanks in advance,
Asha


 
James Ward
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One way could be to strip the html of all tags, and then try to search for the string in it.

This regular expression removes all html tags: (?<=^|>)[^><]+?(?=<|$)

See this link: http://www.gskinner.com/RegExr/
Here you have some readymade regular expressions given on the right hand panel under 'Samples'.
 
Ranch Hand
Posts: 156
Android Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Asha,

You could try this.



I hope this is helpful.
 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

All these methods would be good, if you have a small html code. But as the html content grows, particularly regular expressions would be too heavy for the server to handle (Multipe requests).

I would rather ask you to use some html parsers.

http://mozillaparser.sourceforge.net
http://jerichohtml.sourceforge.net (in the market for quite sometime)

The basic point you need to understand is, html's are nothing but some kind of xmls. So XMl parsing is the ideal way.

Cheers
Aneesh
 
Lookout! Runaway whale! Hide behind this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic