Jump to content

StringRegExpReplace replaces everything in the same place


Go to solution Solved by jguinch,

Recommended Posts

Posted

I'm having a bit of trouble using StringRegExpReplace, I wanted to merge Product column to Description column but StringRegExpReplace replaces all to the first match.
 

$t='<tr><td rowspan="1" colspan="1" data-title="Product">Windows 8</td><td rowspan="1" colspan="1" data-title="Description">Country variant</td><td rowspan="1" colspan="1" data-title="Release">RTM</td></tr><tr><td rowspan="1" colspan="1" data-title="Product">windows 8.1</td><td rowspan="1" colspan="1" data-title="Description">Country variant</td><td rowspan="1" colspan="1" data-title="Release">RTM</td></tr>'
$r = StringRegExpReplace($t,'(?ims)<td .+? data-title="Product">(.+?)</td><td .+? data-title="Release">(.+?)</td>','\1 \[\2\]')
ConsoleWrite(@error&@LF&$r&@LF)
Exit

Output (Red color=Bad replacement):
<tr>Windows 8 [RTM]windows 8.1 [RTM]<td rowspan="1" colspan="1" data-title="Release">RTM</td></tr>
 
Expected (Green color=Good replacement):
<tr><td rowspan="1" colspan="1" data-title="Product">Windows 8 [RTM]</td><td rowspan="1" colspan="1" data-title="Description">Country variant</td><td rowspan="1" colspan="1" data-title="Release">RTM</td></tr><tr><td rowspan="1" colspan="1" data-title="Product">windows 8.1 [RTM]</td><td rowspan="1" colspan="1" data-title="Description">Country variant</td><td rowspan="1" colspan="1" data-title="Release">RTM</td></tr>

I'm doing like this to after use StringRegExp($rTable,'(?ims)<td .+? data-title="Product">(.+?)</td>',3) and capture everything already on Product [Release] format

Heroes, there is no such thing

One day I'll discover what IE.au3 has of special for so many users using it.
C'mon there's InetRead and WinHTTP, way better
happy.png

Posted

I think you have to retrieve the release value before (not sure you can do it in just one shot)

$t='<tr><td rowspan="1" colspan="1" data-title="Product">Windows 8</td><td rowspan="1" colspan="1" data-title="Description">Country variant</td><td rowspan="1" colspan="1" data-title="Release">RTM</td></tr><tr><td rowspan="1" colspan="1" data-title="Product">windows 8.1</td><td rowspan="1" colspan="1" data-title="Description">Country variant</td><td rowspan="1" colspan="1" data-title="Release">RTM</td></tr>'
$r = StringRegExpReplace($t, 'Product">[^<]+\K', ' [' & StringRegExpReplace($t, '(?s).+Release">([^<]+).+', '$1' ) & ']')
ConsoleWrite(@error&@LF&$r&@LF)
Posted

it's working, didn't knew I could intercalate SRER commands 

thought it would run first StringRegExpReplace($t, '(?s).+Release">([^<]+).+', '$1' ) and then the main SRER

Thank you jquinch! :D

Heroes, there is no such thing

One day I'll discover what IE.au3 has of special for so many users using it.
C'mon there's InetRead and WinHTTP, way better
happy.png

Posted

UPS nope, it picks the last return from the inside SRER and uses it for all of them

#include <Array.au3>
$t='<tr><td rowspan="1" colspan="1" data-title="Product">Windows 8</td><td rowspan="1" colspan="1" data-title="Description">Country variant</td><td rowspan="1" colspan="1" data-title="Release">RTM</td></tr><tr><td rowspan="1" colspan="1" data-title="Product">windows 8.1</td><td rowspan="1" colspan="1" data-title="Description">Country variant</td><td rowspan="1" colspan="1" data-title="Release">Technical Preview</td></tr>'
$r = StringRegExpReplace($t, 'Product">[^<]+\K', ' [' & StringRegExpReplace($t, '(?s).+Release">([^<]+).+', '$1' ) & ']')
$os = StringRegExp($r,'(?ims)<td .+? data-title="Product">(.+?)</td>',3)
_ArrayDisplay($os)
Exit

Returns

Row|Col 0
[0]|Windows 8 [Technical Preview]
[1]|windows 8.1 [Technical Preview]

Heroes, there is no such thing

One day I'll discover what IE.au3 has of special for so many users using it.
C'mon there's InetRead and WinHTTP, way better
happy.png

  • Solution
Posted (edited)

Sorry, I didn't understand what was the expected result.

#include <Array.au3>
$t='<tr><td rowspan="1" colspan="1" data-title="Product">Windows 8</td><td rowspan="1" colspan="1" data-title="Description">Country variant</td><td rowspan="1" colspan="1" data-title="Release">RTM</td></tr><tr><td rowspan="1" colspan="1" data-title="Product">windows 8.1</td><td rowspan="1" colspan="1" data-title="Description">Country variant</td><td rowspan="1" colspan="1" data-title="Release">Technical Preview</td></tr>'

$aRet = StringRegExp($t, '"(?:Product|Release)">([^<]+)', 3)
For $i = 0 To UBound($aRet) - 1 Step 2
    ConsoleWrite($aRet[$i] & " [" & $aRet[$i + 1] & "]" & @CRLF)
Next
Edited by jguinch
Posted

 

Sorry, I didn't understand what was the expected result.

#include <Array.au3>
$t='<tr><td rowspan="1" colspan="1" data-title="Product">Windows 8</td><td rowspan="1" colspan="1" data-title="Description">Country variant</td><td rowspan="1" colspan="1" data-title="Release">RTM</td></tr><tr><td rowspan="1" colspan="1" data-title="Product">windows 8.1</td><td rowspan="1" colspan="1" data-title="Description">Country variant</td><td rowspan="1" colspan="1" data-title="Release">Technical Preview</td></tr>'

$aRet = StringRegExp($t, '"(?:Product|Release)">([^<]+)', 3)
For $i = 0 To UBound($aRet) - 1 Step 2
    ConsoleWrite($aRet[$i] & " [" & $aRet[$i + 1] & "]" & @CRLF)
Next

I done almost the same, 1regex for which parameter (product and release) and then with a for loop done the same, is not as time consuming as I expected :)

Thank you jguinch :)

Heroes, there is no such thing

One day I'll discover what IE.au3 has of special for so many users using it.
C'mon there's InetRead and WinHTTP, way better
happy.png

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
  • Recently Browsing   0 members

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