Jump to content
Sign in to follow this  
John117

Help Parsing string

Recommended Posts

John117

hey guys, I need to pull a code from a couple thousand emails I have. The code replaces where you see 'X' in the below example. The location of the code is unknown as far as character length is concerned. The following code worked for another project I had, So I am hoping this one will as well. Please help if you can . . .

#include <file.au3>
#include <IE.au3>
#include <Array.au3>
HotKeySet("{ESC}", "_Quit")
HotKeySet("{NUMPAD1}", "_Run1")

$oIE = _IEAttach ("The Email With The Code")
$sHTML = _IEBodyReadHTML ($oIE)

While 1
Sleep(50)
WEnd

Func _Run1()
;CODE: XXXXX-XXXXX-XXXXX-XXXXX-XXXXX Behold ;This is the string I am looking to pull only the XXX-XXX stuff from. It includes code at the start of it and space and behold at the end. 
$aNameArray = StringRegExp($sHTML, " ([[:alnum:]]*?)\-([[:alnum:]]*?)X", 3) ;This code worked with a similar project with a unknown email address
;$aNameArray = StringRegExp($sHTML, "CODE: ([[:alnum:]]*?) Behold", 0) ;I tried this adaptation
MsgBox(4096, "Working", $aNameArray[0])
;MsgBox(4096, "Working", $sHTML)
EndFunc ;==>Run

Func _Quit()
Exit
EndFunc ;==>Quit
Edited by Hatcheda

Share this post


Link to post
Share on other sites
Richard Robertson

How about the expression "CODE: ([0-9\-]+?) Behold". See if that returns the code.

Share this post


Link to post
Share on other sites
John117

from the example, is that not by position? - that is unknown and changes. - the example I included is the best method as a catch all, but I am not sure how to correctly adapt it for the new string . . .

Share this post


Link to post
Share on other sites
John117

How about the expression "CODE: ([0-9\-]+?) Behold". See if that returns the code.

so try this?

$aNameArray = StringRegExp($sHTML, "CODE: ([0-9\-]+?) Behold", 3)

or

$aNameArray = StringRegExp($sHTML, "CODE: ([0-9\-]+?) Behold", 0)

Share this post


Link to post
Share on other sites
John117

ok, as written does not work. . .

#include <file.au3>
#include <IE.au3>
#include <Array.au3>
HotKeySet("{ESC}", "_Quit")
HotKeySet("{NUMPAD1}", "_Run1")

$oIE = _IEAttach ("The Website")
$sHTML = _IEBodyReadHTML ($oIE)

While 1
    Sleep(50)
WEnd

Func _Run1()
    ;CODE: XXXXX-XXXXX-XXXXX-XXXXX-XXXXX          Behold
;$aNameArray = StringRegExp($sHTML, " ([[:alnum:]]*?)\-([[:alnum:]]*?)X", 3) ;This code worked with a similar project with a unknown email address




$aNameArray = StringRegExp($sHTML, "CODE: ([0-9\-]+?) Behold", 0) 
;$aNameArray = StringRegExp($sHTML, "CODE: ([[:alnum:]]*?)          Behold", 0) ;I tried this adaptation
MsgBox(4096, "Working", $aNameArray)
;MsgBox(4096, "Working", $sHTML)
EndFunc ;==>Run
;_FileWriteToLine($sFile, $iLine, $sText[, $fOverWrite = 0])
Func _Quit()
Exit
EndFunc ;==>Quit
_StringBetween()

Share this post


Link to post
Share on other sites
Varian

I was thinking something like this:

Func _Run1()
    $aNameArray  = _StringBetween($sHTML, 'CODE:', 'Behold');Finds all occurences of "CODE: [i]SomeString [/i]Behold"
    If not IsArray($aNameArray) Then Return
    For $i=0 to ubound($aNameArray) -1
        $ReplacementString = 'CODE: Behold'  ;Whatever your Static string to place is
        If StringInStr($sHTML, $aNameArray[$i]) Then StringReplace($sHTML, $aNameArray[$i], $ReplacementString)
    Next
EndFunc

This should work if you are either removing the XXX stuff or replacing it with the same string each time

Edited by Varian

Share this post


Link to post
Share on other sites
John117

ok, I can get it to show the first two now, but not three

$aNameArray = StringRegExp($sHTML, "CODE: ([[:alnum:]]*?)\-([[:alnum:]]*?)-", 3)

MsgBox(4096, "Working", $aNameArray[0] & "-" & $aNameArray[1])

Edit:

Checking previous post . . . .

Edited by Hatcheda

Share this post


Link to post
Share on other sites
John117

:-) ok, that works! added #String to the top

$aNameArray = _StringBetween($sHTML, 'CODE:', 'Behold');Finds all occurences of "CODE: SomeString Behold"

Share this post


Link to post
Share on other sites
Varian

Yeah, I forgot to mention the includes!

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  

×