Sign in to follow this  
Followers 0
rudi

RegEx Question for scripted HTML code modification

3 posts in this topic

#1 ·  Posted (edited)

Hi.

For a MRTG setup I want to quickly extract all PortChannels to a separate HTML file.

The RegEx matching the links I want to preserve is this: (?i)(?s)(<td><DIV><B>Po.*?</td>)

All other <td>....</td> are to be removed.

I was wondering about nesting a 2nd match for <td>...</td> in a different "bracket level", so that the one I want to preserve will be $1, the other one $2, and then just to "replace $1", so that all the $2 will be dropped.

This must be easy, I just can't see it :graduated:

$index="\\nw04goe\sys\apache2\htdocs\mrtg\index.html"
$PO_Only="\\nw04goe\sys\apache2\htdocs\mrtg\index-PO.html"
Dim $aIndex

$Content=FileRead($index)
FileWrite($PO_Only,StringRegExpReplace($Content,"?????????????????","$?$?")

Regards, Rudi.

Edited by rudi

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Share this post


Link to post
Share on other sites



It isn't clear to me if this is "easy".

Rewriting your conditions we have that you want to replace any <td>....</td> which doesn't contain a match to (?i)(?s)(<td><DIV><B>Po.*?</td>) by an empty string.

But what do you do with the rest?

If it was me, I'd extract the matching TDs using this pattern with StringRegExp in mode 3 and I would insert the captures (array) into a clean homebrew html squeleton. That would keep you safe if ever the structure of the input html changes, as long as the valuable TDs continue to match the simple pattern you use. You also are free to manage the output html as you wish.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Hi.

Thanks for your reply.

I was wondering about using stringregex(), mode 3 as well, but I would loose all the other HTML content.

This is, how it works really nice:

$index = "\\nw04goe\sys\apache2\htdocs\mrtg\index.html"
$PO_Only = "\\nw04goe\sys\apache2\htdocs\mrtg\index-PO.html"
Dim $aIndex



$Content = FileRead($index)
$Content = StringRegExpReplace($Content, "(?i)(?s)((<td><DIV><B>Po.*?</td>)|<td><DIV><B>.*?</td>)", "$2")
FileWrite($PO_Only,StringRegExpReplace($Content,"(?i)(?s)(<tr>\s*</tr>)","")) ; remove empty table rows: <tr> 0 to unlimited whitespace </tr>

ShellExecute($PO_Only)

As I remember, how hard RegEx was in the beginning, I'll spend a few minutes, to explain it to beginners, that might come across this example. I remember very well, that most RegEx examples I came across were not readable to me.

(?i)(?s)((<td><DIV><B>Po.*?</td>)|<td><DIV><B>.*?</td>)", "$2"
(?i) = case insensitive
    (?s) Dot (".") does match new lines (the match doesn't care about line breakes in the HTML code)

        (                        |                    ) = the whole match goes to backreference 1, which would be addressed using "$1" (in stead of "$2") in the replace statement.
        (                        |<td><DIV><B>.*?</td>) = all the "Rule 2 matches" go to BackRef 1. Rule 2 is only checked, when Rule 1 *DIDN'T* match, so it doesn't matter
                                                          that Rule 2 also covers all stuff matched by Rule 1: If Rule 1 is matched, Rule 2 won't be checked.
        ( <td><DIV><B>Po.*?</td> |                    ) = that way Rule 1 would go to BackRef1 as well, so no way to distinguish.
        ((<td><DIV><B>Po.*?</td>)|                    ) = the extra brackets make "Rule 1 matches" to go to BackRef 2, $2 in the replace statement. Distinguish done! (nested grouping)
                                                          Note: Rule 1 matches *ALSO* go to BackRef 1, as the hole match goes to BackRef 1. (Just leave it unused :-))

.*? = match any character("."), as *LITTLE* times as needed("*?"). (Lazy). Using "*" (and not "*?") it would be "greedy" (match-as-much-as-possible), 
so it would match everything from the first opening "<td>..." to the last closing "...</td>" in the *WHOLE* HTML code

Regards, Rudi.

[edit: Typos]

Edited by rudi

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0