html
php
css
c
ajax
python
mysql
linux
ruby-on-rails
regex
silverlight
html5
algorithm
cocoa
tsql
delphi
php5
asp
postgresql
I would use something like that:
import java.util.StringTokenizer; import java.util.regex.Matcher; import java.util.regex.Pattern; class SimpleRegexExample { //TODO: Load your html content from file protected String html = "<html>\n" + "<head>\n" + " <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\n" + " <meta http-equiv=\"Content-Style-Type\" content=\"text/css\">\n" + " <title></title>\n" + " <meta name=\"Generator\" content=\"Cocoa HTML Writer\">\n" + " <meta name=\"CocoaVersion\" content=\"1038.36\">\n" + " <style type=\"text/css\">\n" + " p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 26.0px Helvetica}\n" + " p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 19.0px Helvetica}\n" + " p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px}\n" + " p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 19.0px Helvetica; min-height: 23.0px}\n" + " p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica}\n" + " </style>\n" + "</head>\n" + "\n" + " <body>\n" + " <p class=\"p1\"><b>{{BookName}}</b></p>\n" + " <p class=\"p2\">by {{AuthorName}}</p>\n" + " <p class=\"p3\"><br></p>\n" + " <p class=\"p3\"><br></p>\n" + " <p class=\"p4\"><span class=\"Apple-converted-space\"> </span></p>\n" + " <p class=\"p5\">{{AuthorBio}}</p>\n" + " <p class=\"p3\"><br></p>\n" + " <p class=\"p3\"><br></p>\n" + " <p class=\"p5\">{{Credits}}</p>\n" + " <p class=\"p3\"><br></p>\n" + " <p class=\"p3\"><br></p>\n" + " </body>\n" + "</html>\n"; public SimpleRegexExample() { // Split the html text into single lines StringTokenizer st = new StringTokenizer(html, "\n"); // Initialize the regex String regex = "\\{\\{(.*?)\\}\\}"; Pattern p = Pattern.compile(regex); // Parses each line while (st.hasMoreTokens()) { String token = st.nextToken(); Matcher m = p.matcher(token); // Searches for matches while (m.find()) { if (m.group(1) != null) { // TODO: Set here your replacement html = html.replace("{{" + m.group(1) + "}}", m.group(1)); } } } // Prints the result System.out.println(html); } public static void main(String[] args) { new SimpleRegexExample(); } }
A regular expressions is used to define and search for a pattern of text within a string.
Just check below link, a stackoverflow question with good explanation about Regular Expression usage.
How to use a regular expression and assign the result to variables in Android?
I hope it may help you.
to get content from xml/html the best way possible is XPath you can direct have a query to get the Element in DOM which contain your required data.
If the HTML is your content then i will suggest u to have an attribute which specify that it is book name.
you can use the XPath query like this
//p[@class='p1']/b
Condition for XPath is that the HTML file should have a proper closing Tag.
But if you want to use Regular Expression then use this
.+<p class=\"p1">
This expression will the match for class p1 and u need to get the end index for this expression to get the position of the b tag after p tag.
U can try your expression at
http://www.gskinner.com/RegExr/