RegEx Multiline result - Netbeans 7.2 find and replace feature
Asked Answered
A

1

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.

Autosome answered 29/5, 2013 at 18:31 Comment(2)
This is one of the bagillion regExs I've tried : td(.*\s)*</td>Autosome
you can try <td>.*?</td>Hectic
S
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

Salmagundi answered 29/5, 2013 at 19:30 Comment(3)
That works great : (?s)(<td[^>]*>.*?<\/td>) althought the backslash in "<\/td>" isn't necessary but... i get the principle!Autosome
@Autosome I was doubting about it :pSalmagundi
+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.Kuching

© 2022 - 2024 — McMap. All rights reserved.