0

I have a string like this (made from HTML source code):

<tr>
  <td>
    <tr>First</tr>
  </td>
</tr>
<tr>
  <td>Second</td>
</tr>
<tr>
  <td>
    <tr>
      <td>Upper</td>
    </tr>
    <tr>
      <td>Lower</td>
    </tr>
  </td>
</tr>

but in one line - I divided it to make it look better. What I want to achieve is a regular expression that will capture whole rows of this table, so the matches are:

<td>
  <tr>First</tr>
</td>

,

<td>Second</td>

,

<td>
  <tr>
    <td>Upper</td>
  </tr>
  <tr>
    <td>Lower</td>
  </tr>
</td>

The most simple options:

  • <tr>.*</tr> - catches everything
  • <tr>.*?</tr> - catches from the first <tr> to the first </tr>.

I want it to catch corresponding tags. Can anybody help?

10
  • 5
    Use an HTML parser to parse HTML. And in future, please review the preview carefully before posting. Commented Jun 13, 2013 at 12:30
  • 2
    Use something like JSoup or you'll get burned Commented Jun 13, 2013 at 12:32
  • 2
    Relevant: stackoverflow.com/questions/238036/java-html-parsing Commented Jun 13, 2013 at 12:32
  • 2
    You should not use a regex for parsing HTML. This answer provides a fantastic explanation why you shouldn't do it: stackoverflow.com/questions/1732348/… Commented Jun 13, 2013 at 12:32
  • 2
    @AndrewThompson I think SO should put this link in the face of the user if it sees regex and html as a tag combination ;) Commented Jun 13, 2013 at 12:34

1 Answer 1

1

You could use html parsing engine jsoup and run something like this to pull out rows from your table

String url = "a.html";
Document doc = Jsoup.connect(url).get();

Elements rows = doc.select("table tr");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.