6

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.

assylias
  • 321,522
  • 82
  • 660
  • 783
Kraven
  • 233
  • 2
  • 11

1 Answers1

13

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

HamZa
  • 14,671
  • 11
  • 54
  • 75
  • 1
    That works great : (?s)(]*>.*?<\/td>) althought the backslash in "<\/td>" isn't necessary but... i get the principle! – Kraven May 29 '13 at 19:39
  • @Kraven I was doubting about it :p – HamZa May 29 '13 at 19:47
  • 1
    +1 helped me getting my span opening and closing which in a new line. Thanks. This answer is number two in google result when searching for "netbeans regex new line" So maybe adding an extra general solution will be make it get more views. – Mohammed Joraid Jul 24 '14 at 12:24