Jump to content

A little help with stringregexreplace() please!


Recommended Posts

hi people, fine?

i need a little help with my code

i need to replace the red text to blue text!

------------------------------------------

this:

<tr bgcolor=#D5BCCD>

<td>1</td>

<td>29/09/2003</td>

<td>18</td>

<td>20</td>

<td>25</td>

<td>23</td>

<td>10</td>

<td>11</td>

<td>24</td>

<td>14</td>

<td>06</td>

<td>02</td>

<td>13</td>

<td>09</td>

<td>05</td>

<td>16</td>

<td>03</td>

<td>0,00</td>

<td>5</td>

<td>154</td>

<td>4645</td>

<td>48807</td>

<td>257593</td>

<td>49.765,82</td>

<td>689,84</td>

<td>10,00</td>

<td>4,00</td>

<td>2,00</td>

<td>0,00</td>

<td>0,00</td>

</tr>

<tr>

<td>2</td>

<td>06/10/2003</td>

<td>23</td>

<td>15</td>

<td>05</td>

<td>04</td>

<td>12</td>

<td>16</td>

<td>20</td>

<td>06</td>

<td>11</td>

<td>19</td>

<td>24</td>

<td>01</td>

<td>09</td>

<td>13</td>

<td>07</td>

<td>0,00</td>

<td>1</td>

<td>184</td>

<td>6232</td>

<td>81252</td>

<td>478188</td>

<td>596.323,70</td>

<td>1.388,95</td>

<td>10,00</td>

<td>4,00</td>

<td>2,00</td>

<td>0,00</td>

<td>0,00</td>

</tr>

------------------------------------------------------------------------

to this:

<tr bgcolor=#D5BCCD>

<td>1</td>

<td>29/09/2003</td>

<jg>18,20,25,23,10,11,24,14,06,02,13,09,05,16,03</jg>

<td>0,00</td>

<td>5</td>

<td>154</td>

<td>4645</td>

<td>48807</td>

<td>257593</td>

<td>49.765,82</td>

<td>689,84</td>

<td>10,00</td>

<td>4,00</td>

<td>2,00</td>

<td>0,00</td>

<td>0,00</td>

</tr>

<tr>

<td>2</td>

<td>06/10/2003</td>

<jg>23,15,05,04,12,16,20,06,11,19,24,01,09,13,07</jg>

<td>0,00</td>

<td>1</td>

<td>184</td>

<td>6232</td>

<td>81252</td>

<td>478188</td>

<td>596.323,70</td>

<td>1.388,95</td>

<td>10,00</td>

<td>4,00</td>

<td>2,00</td>

<td>0,00</td>

<td>0,00</td>

</tr>

i've maded this:

Local $read = FileRead(@TempDir & "\" & $htmlfilename)
    $read = StringRegExpReplace($read, "<td>(\d{2})</td>\s*?<td>(\d{2})</td>\s*?<td>(\d{2})</td>\s*?<td>(\d{2})</td>\s*?<td>(\d{2})</td>\s*?<td>(\d{2})</td>\s*?<td>(\d{2})</td>\s*?<td>(\d{2})</td>\s*?<td>(\d{2})</td>\s*?<td>(\d{2})</td>\s*?<td>(\d{2})</td>\s*?<td>(\d{2})</td>\s*?<td>(\d{2})</td>\s*?<td>(\d{2})</td>\s*?<td>(\d{2})</td>\s*?", "<jg>$1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15</jg>")

worked, but... i think it was "strange"

the code was too big, so i guess i'm not knowing to use regular expressions!

thanks a lot if you help me!

Edited by darkshark
Link to comment
Share on other sites

The problem with this approach is that it relies on a strict format and a fixed number of entries in the <jg> part.

If that's OK for your use case, then it's as "simple" as you can hope in a one-liner.

You can have the same transformation without the above limitations by first capturing every instances in an array (option 3) then working with the usual String* function to insert it where needed.

It will get less simple anyway, so if you have that fixed number of entries, stick with that pattern.

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)

Link to comment
Share on other sites

I agree with jchd in that your expression will work under exactly the same circumstances you show but you may want get the sections into an array first to allow some flexibility in the future. Here is an example I just whipped up briefly where src.txt just contains what you posted in your first quote section.

$sSrc = FileRead("src.txt")
$aSrc = StringRegExp($sSrc, "(?is)<td>\d{2}/\d{2}/\d{4}</td>\v+(.+?)<td>\d,\d+</td>", 3)
If NOT @Error Then
    For $i = 0 To UBound($aSrc) -1
        Local $sHold = ""
        $sHold &= "<jg>" & StringTrimRight(StringRegExpReplace($aSrc[$i], "(?i)(?m:^)<td>(\d+)</td>(?:\v|$)+", "$1,"), 1) & "</jg>" & @LF & @LF;;  For HTML use @LF NOT @CRLF
        $sSrc = StringReplace($sSrc, $aSrc[$i], $sHold)
    Next
EndIf
ClipPut($sSrc);  Just to show the result-- a FileOpen/FileWrite/FileClose is probably better here.

I'm sure it can be made even more efficient but as it is it will allow the flexibility.

Edit: Forgot your non-functional <jg></jg> tags which I assume are there for future editing/parsing purposes.

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Do you mind if I ask the purpose of the transformation? It looks like the TR/TD data for an HTML table, except that "jg" is not a valid tag to be inserting there. One reason I ask is just curiosity: What the heck is a "jg" tag for?. The other reason is that if you are extracting the html from a page table and modifying it just to write back to the page, you do it on the DOM like this:

#include <IE.au3>

_IEErrorHandlerRegister()

Global $s_html, $oIE, $oTable, $tr, $oTR, $td
Global $aJG_Tags[2] = ["<jg>18,20,25,23,10,11,24,14,06,02,13,09,05,16,03</jg>", _
        "<jg>23,15,05,04,12,16,20,06,11,19,24,01,09,13,07</jg>"]
_CreateHtml()

$oIE = _IECreate()
_IEDocWriteHTML($oIE, $s_html)
Sleep(5000) ; Delay to see 'before'

$oTable = _IETableGetCollection($oIE, 0)
For $tr = 0 To 1
    $oTR = _IETagNameGetCollection($oTable, "tr", $tr)
    For $td = 16 To 2 Step -1
        $oTR.deleteCell($td)
        Sleep(250)
    Next
    $oTR.insertCell(2)
    $oTR.Cells(2).innerHTML = $aJG_Tags[$tr]
Next

Func _CreateHtml()
    $s_html = "<HTML>" & @CR
    $s_html &= "<BODY>" & @CR
    $s_html &= "<table id='tableOne'>" & @CR
    $s_html &= "    <tr bgcolor=#D5BCCD>" & @CR
    $s_html &= "        <td>1</td>" & @CR
    $s_html &= "        <td>29/09/2003</td>" & @CR
    $s_html &= "        <td>18</td>" & @CR
    $s_html &= "        <td>20</td>" & @CR
    $s_html &= "        <td>25</td>" & @CR
    $s_html &= "        <td>23</td>" & @CR
    $s_html &= "        <td>10</td>" & @CR
    $s_html &= "        <td>11</td>" & @CR
    $s_html &= "        <td>24</td>" & @CR
    $s_html &= "        <td>14</td>" & @CR
    $s_html &= "        <td>06</td>" & @CR
    $s_html &= "        <td>02</td>" & @CR
    $s_html &= "        <td>13</td>" & @CR
    $s_html &= "        <td>09</td>" & @CR
    $s_html &= "        <td>05</td>" & @CR
    $s_html &= "        <td>16</td>" & @CR
    $s_html &= "        <td>03</td>" & @CR
    $s_html &= "        <td>0,00</td>" & @CR
    $s_html &= "        <td>5</td>" & @CR
    $s_html &= "        <td>154</td>" & @CR
    $s_html &= "        <td>4645</td>" & @CR
    $s_html &= "        <td>48807</td>" & @CR
    $s_html &= "        <td>257593</td>" & @CR
    $s_html &= "        <td>49.765,82</td>" & @CR
    $s_html &= "        <td>689,84</td>" & @CR
    $s_html &= "        <td>10,00</td>" & @CR
    $s_html &= "        <td>4,00</td>" & @CR
    $s_html &= "        <td>2,00</td>" & @CR
    $s_html &= "        <td>0,00</td>" & @CR
    $s_html &= "        <td>0,00</td>" & @CR
    $s_html &= "    </tr>" & @CR
    $s_html &= "    <tr>" & @CR
    $s_html &= "        <td>2</td>" & @CR
    $s_html &= "        <td>06/10/2003</td>" & @CR
    $s_html &= "        <td>23</td>" & @CR
    $s_html &= "        <td>15</td>" & @CR
    $s_html &= "        <td>05</td>" & @CR
    $s_html &= "        <td>04</td>" & @CR
    $s_html &= "        <td>12</td>" & @CR
    $s_html &= "        <td>16</td>" & @CR
    $s_html &= "        <td>20</td>" & @CR
    $s_html &= "        <td>06</td>" & @CR
    $s_html &= "        <td>11</td>" & @CR
    $s_html &= "        <td>19</td>" & @CR
    $s_html &= "        <td>24</td>" & @CR
    $s_html &= "        <td>01</td>" & @CR
    $s_html &= "        <td>09</td>" & @CR
    $s_html &= "        <td>13</td>" & @CR
    $s_html &= "        <td>07</td>" & @CR
    $s_html &= "        <td>0,00</td>" & @CR
    $s_html &= "        <td>1</td>" & @CR
    $s_html &= "        <td>184</td>" & @CR
    $s_html &= "        <td>6232</td>" & @CR
    $s_html &= "        <td>81252</td>" & @CR
    $s_html &= "        <td>478188</td>" & @CR
    $s_html &= "        <td>596.323,70</td>" & @CR
    $s_html &= "        <td>1.388,95</td>" & @CR
    $s_html &= "        <td>10,00</td>" & @CR
    $s_html &= "        <td>4,00</td>" & @CR
    $s_html &= "        <td>2,00</td>" & @CR
    $s_html &= "        <td>0,00</td>" & @CR
    $s_html &= "        <td>0,00</td>" & @CR
    $s_html &= "    </tr>" & @CR
    $s_html &= "</table>" & @CR
    $s_html &= "</BODY>" & @CR
    $s_html &= "</HTML>"
EndFunc   ;==>_CreateHtml
The Sleeps are in there to slow it down enough to see what happens. This actually puts the "jg" tag inside the third "td" tag of the row, since "jg" is not valid.

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

thanks all for the repply!

Do you mind if I ask the purpose of the transformation? It looks like the TR/TD data for an HTML table, except that "jg" is not a valid tag to be inserting there. One reason I ask is just curiosity: What the heck is a "jg" tag for?. The other reason is that if you are extracting the html from a page table and modifying it just to write back to the page, you do it on the DOM like this:

<jg> the tag is just an example, i am doing tests, but it is not a permanent tag, put it to exemplify just what i wanted!

thanks a lot for the code

The problem with this approach is that it relies on a strict format and a fixed number of entries in the <jg> part.

If that's OK for your use case, then it's as "simple" as you can hope in a one-liner.

You can have the same transformation without the above limitations by first capturing every instances in an array (option 3) then working with the usual String* function to insert it where needed.

It will get less simple anyway, so if you have that fixed number of entries, stick with that pattern.

yeah, u're right, thanks a lot man!

@GEOSoft

thanks a lot for the code too!

Edited by darkshark
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...