RegEx Multiline result - Netbeans 7.2 find and replace feature - regex

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.

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

Related

Collect results from groups to one string

I want to parse weather html page for Openhab.
This is significant part of whole html:
<!-- Amount of Sun -->
<tr>
<td class="label_det">
<span class="sum">∑</span> <span class="unit">in u</span>
</td>
<td class="sunamount">
10.2
</td>
<td class="sunamount">
10.6
</td>
<td class="sunamount">
5.9
</td>
<td class="sunamount">
6.8
</td>
<td class="dgrey sunamount">
6.8
</td>
<td class="dgrey sunamount">
5.4
</td>
<td class="sunamount">
5
</td>
</tr>
I would like to collect all numbers into one string, I understand that it's, perhaps, not possible, but may be...
So something like this: '10.2 10.6 5.9 6.8 6.8 5.4 5'
Example of full html and my current regex is here: https://regex101.com/r/nrzPHU/1
Thanks in advice.
You need named capture groups. Named capture groups allow you to specify a given part in regex with a name to extract it later. A named capture group starts with (? then followed by the regex and ended with ).
<td class\=\".*?sunamount\">\s+(?<amount>\d+(\.\d+)?)\s+<\/td>
You would then be able to extract the amount by applying your regex to the input and picking the group named amount out of it.
Reading about OpenHab online I'm not sure they support named capture groups. So an alternative would be using the regex above to match all lines with amounts in the input. Then using a regex replace on that matched string. So something like...
Use this regex to get amounts:
<td class\=\".*?sunamount\">\s+\d+(\.\d+)?\s+<\/td>
Use this regex on the result of the regex above to replace the non amounts (and replace them with an empty string to delete them):
([\s]|<td class=".*?">|<\/td>)

Tree-like matches in regex with a fixed chain

i have a very specific task to achieve with a single regex.
Here's the pattern of the text i have to extract the data from (note i'm parsing HTML-like code, stored in an immutable file) :
<tr>
<td > <a ><img /></a>
</td>
<td > <a ><span >RootData</span></a>
</td>
<td > Data1.1
</td>
<td > <a ><img /></a>
</td>
<td > <a ><span >Data1.2</span></a>
</td>
<td >  
</td></tr>
<tr>
<td > Data2.1
</td>
<td > <a ><img /></a>
</td>
<td > <a ><span >Data2.2</span></a>
</td>
<td >  
</td></tr>
...
First there's a root contained inside the first "tr". Still inside this one, there's some datq (Data1.1 and Data1.2) to extract.
Then comes a finite number of "tr" block each containing data to extract.
I'd like the matches to be like this :
match 1 : 'RootData' 'Data1.1' 'Data1.2'
match 2 : 'RootData' 'Data2.1' 'Data2.2'
etc
So far i see what to do with 2 regex and 2 loops (like 1 searching for the Root, and the other to find all datas from this root) but i'd like it to be in a single regex.
If some of you already encountered that and could help, that'd be nice :)
Thanks in advance.
If I understand you correctly, you'd like to have a single regular expression provide more than one match for the same input. Regular expressions do not work that way, and are probably just not the right tool for the problem you're trying to solve.

Find a table's last cell by regular expression

I want to use Regular Expression (compatible with pcre) to select a table
cell in an XML or HTML file.This cell was expanded in several lines containing
other elements and relative attributes and values. Thiscell supposed to be at the last column.
for some reasons I can't and don't want to use ". matches newline" option.
for example in this code:
EDITED:
<table colcount="4">
<tr>
<td colspan="2">
<para><text> Mike</text></para>
</td>
<td>
<tab />
</td>
<td1>
<para><text>Jack</text></para>
<para><text>Sarah</text></para>
</td>
</tr1>
<tr>
<td>
<para><text>Bob</text></para>
<para><text>Rita</text></para>
</td>
<td2 colspan="3" with>
<para><text>Helen</text></para>
</td>
</tr2>
<tr>
<td style="with:445px;">
<para><text>Sam</text></para>
</td>
<td>
<para><text>Emma</text></para>
<para><text>George</text></para>
</td>
<td>
</td>
<td3 colspan="">
<tab />
</td>
</tr3>
</table>
/EDITED
I want to find and select the whole last cell together with its start and end tags (<td and </td>)
and the end tag of the corresponding row(</tr>), that is:
EDITED:
Here is what I want to select in the table like above using RegEx:
Either from <td1 to </tr1> - or from <td2 to </tr2> - or from <td3 to </tr3>
/EDITED
The format (indentation and new lines have to be preserved), I mean I can't put, for example
</tr> in front of of closing tag of the cell(</td>).
Indentation is only space character.
Thanks for any help...
Best you can do with regex is:
<td(([^<]|<(?!\/td>))*)<\/td>\s*<\/tr>(?!(.|\r|\n)*<tr)
But this is kinda ugly, resource intensive and breaks when you have nested tables. A better route is indeed to use an XML or HTML parser for whichever programming language you're using.
If you want to select the last cell from EVERY row, as your updated question suggests, leave out the negative lookahead like so:
<td(([^<]|<(?!\/td>))*)<\/td>\s*<\/tr>
Working example here: http://refiddle.com/gt2

Delete text block with regexp in notepad++

Textfile contains blocks:
<tr>
<td>waitForElementPresent</td>
<td>//div</td>
<td></td>
</tr>
<tr>
<td>assertElementPresent</td>
<td>//div</td>
<td></td>
</tr>
I want to remove all blocks with regexp in notepad++ with the word assertElementPresent in it:
<tr>*assertElementPresent*</tr>
who can help me with the regular expression??
<tr>.*?assertElementPresent.*?</tr>
should be a good start (note the ungreedy matches), however, it's rather brittle.
<tr>(?!.*<tr>.*).*?assertElementPresent.*?</tr>
It has been tested with RAD and RegExr. The previous suggested solution picks the previous row also..

Notepad++ search & replace

I'm trying to convert a html file with 100 of entries like this one:
<table>
<tr>
<td valign="top" width="30">
1.</td>
<td>
TEXT DESCRIPTION
</td>
</tr>
</table>
<table><tr><td></td></tr></table>
where the number "1." goes from 1 to 100, into this:
<li>
TEXT DESCRIPTION
</li>
I haven't find a way to do this, neither with regexp nor with extended search mode. Any ideas?
You could start with this:
Replace
.*<td>(.*[A-Za-z]+.*)<\/td>.*
with
<li>\1</li>
This will match one chunk of code of the form you reported. You must modify it to match multiple chunks of the same form in the same file.
Moreover to work correctly we should make it match lazily. Someone who knows how?