Forums Register Login

Regular Expression any char between html-tags

+Pie Number of slices to send: Send
I want to fetch any data between a <td>sart tag and </td>end tag. I only find examples for getting the tags and not the data...
+Pie Number of slices to send: Send
Hi,

You don't say whether or not you have other tags within your <td> tags, whether or not there can be a new line within the <td> tag or whether or not you can have more than one on one line so take the following as a starting point.

import java.util.regex.*;

public class Test20040913
{
public static void main(String[] args)
{
Pattern pattern = Pattern.compile("<td>([^<]*)</td>", Pattern.MULTILINE);

String lines = "some rubbish <td>value 1</td> some futher \nrubbish <td>value \n2</td> and more again";
Matcher matcher = pattern.matcher(lines);
for (int startPoint = 0; matcher.find(startPoint); startPoint = matcher.end())
{
System.out.println(" Value found at " + matcher.start(1) + " with value [" + matcher.group(1) + "]");
}
}
}
+Pie Number of slices to send: Send
Well in my td tag there wont be another htmltag. No new lines, just a-z&0-9.. your code didnt do the trick. Here is my String


The value I want is "7 r", "2 st" & "6 194 834 p".

Thansk for your time!
+Pie Number of slices to send: Send
This might be overkill but you could use an xml parser and get the values from the 'TD' elements.
+Pie Number of slices to send: Send
Change the pattern to

Pattern pattern = Pattern.compile("<td[^>]*>([^<]*)</td>", Pattern.MULTILINE | Pattern.CASE_INSENSITIVE);

This still makes several severe assumptions about your requirements. One can only generate an effective regular expression if the requirements are well specified and without a good specification one is only guessing.

You are reaching the point where you might do better to use a tolerant XML parser to generate a DOM document. A Google search could be effective.
+Pie Number of slices to send: Send
Sorry Rovas, I had my blinkers on and I didn't spot your post!

It looks like you and I agree about it possible being better to use an XML parser!
No matter. Try again. Fail again. Fail better. This time, do it with this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 3498 times.
Similar Threads
to show the Report
(JSTL) iterate - foreach eqivilant
bean:write tag - query
help with html radio tag
how to add checkbox html tag?- urgent!
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 04:03:46.