Question

I'm using the "find" tool in Netbeans 7.2 and I'm looking to make regular expression that would allow me to gather results that have multiple lines.

A sample Html code I would like to apply the regular expression on :

<tr>
    <td>
        <label>some label</label><span>*</span>
    </td>
    <td>
        <label>some label</label><span>*</span>
        <label>some label</label>
        <label>some label</label>
    </td>
</tr>

Basically, I want to gather any <td> tag including it's content and it's end tag </td>.

In the above example, my first result should be :

<td>
    <label>some label</label><span>*</span>
</td>

and my second result expected would be :

<td>
    <label>some label</label><span>*</span>
    <label>some label</label>
    <label>some label</label>
</td>

I've tried many different regular expressions that would pick up the start of the <td> and the next line (if the <td>'s content is on more than one line).

Example : <td>.*(.*\s*).*

But I'm looking to get a regular expression that can pick up every <td> tags and their content no matter how many <label> tags they hold.

Was it helpful?

Solution

You have to use use the s modifier to match new lines with a dot, I don't know where you can do this in NetBeans but you could begin your expression with (?s) to enable it.

So a regex to match <td ...> ... </td> would be something like this
(?s)(<td[^>]*>.*?<\/td>).

Explanation:

  • (?s) : make the dot match also newlines
  • <td : match <td
  • [^>]* : match everything except > 0 or more times
  • > : match >
  • .*? : match everything 0 or more times ungreedy (until </td> found in this case)
  • <\/td> : should I even explain o_o ?

Online demo

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top