Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RegEx Multiline result - Netbeans 7.2 find and replace feature

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.

like image 647
Kraven Avatar asked May 29 '13 18:05

Kraven


1 Answers

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

like image 104
HamZa Avatar answered Oct 19 '22 23:10

HamZa