Delete text block with regexp in notepad++ - regex

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..

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>)

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

sed replace html

Having difficulty getting a sed search and replace working with an html file.
I have multiple sections that look like this:
<TABLE class="cattable">
<TBODY>
<TR>
<TH colspan="2">Header</TH></TR>
<TR>
<TD>Random Amount of Data</TD>
<TD>3</TD></TR>
<TR>
<TD>Moar Data</TD>
<TD>3</TD></TR>
<TR>
<TD>Yup, More</TD>
<TD>4</TD></TR></TBODY></TABLE>
I need to:
replace with xxxxFOOxxxx:
<TABLE class="cattable">
<TBODY>
<TR>
<TH colspan="2">
keep this:
Header
Replace this with yyyyFOOyyyy:
</TH></TR>
Keep this:
<TR>
<TD>Random Amount of Data</TD>
<TD>3</TD></TR>
<TR>
<TD>Moar Data</TD>
<TD>3</TD></TR>
<TR>
<TD>Yup, More</TD>
<TD>4</TD></TR>
replace this with zzzzFOOzzzz:
</TBODY></TABLE>
Heres what I’ve tried in vim, but cant limit the greedy .* properly:
s:\(<TABLE class="cattable">\_s\s*<TBODY>\_s\s*<TR>\_s\s*<TH colspan="2">\)\(.*\)\(<\/TH><\/TR>\)\(\_.*[^<]*\)\(<\/TABLE>\):xxxxFOOxxxx\2yyyyFOOyyyy\4zzzzFOOzzz<br>:g
tia
Replace * with \{-} to get non-greedy match (same as *? in PCRE/Perl regexes). For more complicated cases you will have to use something with negative look-aheads/look-behinds: like \(\(<\/TH><\/TR>\)\#!.\)* in place of .*, here .\{-} is probably enough.
Note: won’t work in sed.
Note 2: vim is not using BRE or ERE (basic/extended regular expressions) like sed does, vim :s is not invoking any external programs (including sed) and neither your attempt is suitable for sed. Thus if you did not mean to ask how to do this in sed remove sed from tags.

Something about regular expression

If I want to get the current price 416.00 of the following content, what regexp I can use to get it? There are some places in the webpage with similar content, except the one I want has the word Discount in a few lines after the current price. 416,520 and 20% are variable. Thanks.
<tr>
<td class="txt_11px_b_EB6495" width="50" nowrap>Current Price?</td>
<td class="txt_11px_b_EB6495">HK$ 416.00</td>
</tr>
<tr>
<td class="txt_11px_n_999999">Original price?</td>
<td class="txt_11px_n_999999">HK$ 520.00</td>
</tr>
<tr>
<td class="txt_9px_n_999999"> </td>
<td class="txt_9px_n_999999">Discount 20%</td>
</tr>
You can use
" (\d+\.\d*)</td>"
That will match 520.00, 2.00, 123.1, and 123.
Use a HTML parser to get the text node, then extract the price using a regex.
You would use something like...
\d+(?:\.\d{2}|%)
I just tested it and it matched...
416.00
520.00
20%
I assumed (it was unclear to me) you want the prices and the percentage discount. I also matched the % so you can tell what are the percentages in the matches.

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?