Jump to content

How Do I Search For 2 Strings and Extract Whats In Between Them?


Recommended Posts

iGoogle.txtiGoogle.txtI affixed a document to the post called igoogle.....

I want a script that opens up this text document, searches for......all the text that reads "Location: United States" as well as all the text that says "View full report Google Insights". Once the script finds "Location: United States" and "View full report Google Insights" I want all the text thats between "Location: United States" and "View full report Google Insights" extracted and placed in another document. Or a script that deletes everything else but whats between "Location: United States" and "View full report Google Insights" .

Link to comment
Share on other sites

Since both of those lines appear more than once you should be a bit more specific. Do you only want the first occurrence (Telecommunications category) or do you want all of them returned separately?

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

Since both of those lines appear more than once you should be a bit more specific. Do you only want the first occurrence (Telecommunications category) or do you want all of them returned separately?

Yes, I want all them separately. For example........If the document says

Category: Telecommunications

Location: United States

verizon

iphone

blackberry

verizon wireless

att

at&t

twitter

sprint

tmobile

comcast

View full reportGoogle Insights for SearchRestore

I want the script to look for "Location: United States" and "View full reportGoogle Insights for SearchRestore". I want

verizon

iphone

blackberry

verizon wireless

att

at&t

twitter

sprint

tmobile

comcast

to be extracted. Or if the script sees......

Top Searches in the last 7 days

Property: Product Search

Category: Automotive

car

honda

tires

toyota

rims

motorcycle

scooter

bmw

mustang

auto

View full report

Google Insights for SearchRestore

I want the .........

car

honda

tires

toyota

rims

motorcycle

scooter

bmw

mustang

auto

extracted.

Link to comment
Share on other sites

Opt("ExpandVarStrings", 1)

Dim $sTxt = FileRead(@ScriptDir & '\igoogle.txt')

Dim $sFirst1 = 'Location: United States'
Dim $sFirst2 = 'View full reportGoogle Insights for SearchRestore'
Dim $sSecond1 = 'Category: Automotive'
Dim $sSecond2 = 'View full report'
Dim $sPatt = '(?s)(?:(\Q$sFirst1$\E)|(\Q$sSecond1$\E))(.*?)((?1)\Q$sFirst2$\E|\Q$sSecond2$\E)'

Dim $avMatch = StringRegExp($sTxt, $sPatt, 3)

If IsArray($avMatch) Then
    #cs
    For $i = 0 To UBound($avMatch)-1
        ConsoleWrite($avMatch[$i] & @LF)
    Next
    #ce
    
    For $i = 0 To UBound($avMatch)-4 Step 4
        MsgBox(0x40, 'Set #' & $i/4+1, $avMatch[$i+2])
    Next
EndIf

Edit: Fixed, step of 4 was required.

Edited by Authenticity
Link to comment
Share on other sites

Thanks.... I want to output here...D:\Documents and Settings\Taevon Jones\Desktop\New Text Document

How can I go about that?

I know I have to shellexecute like such......

shellexecute("D:\Documents and Settings\Taevon Jones\Desktop\New Text Document")

But after I open the document whats the best function to use to paste all the info that was extracted?

Link to comment
Share on other sites

Read the help file for the functions FileOpen, FileWrite, FileClose and their descriptions. You just need to use FileOpen() to get a handle to a file in writing mode and write the formatted string you want using the FileWrite() function. The loop iterating the array in a way that the info you've requested is in the $i+2 element (i.e $avArray[$i+2]) as shown in the code.

Edited by Authenticity
Link to comment
Share on other sites

This is what I got so far , but it still doesn't output the info like a want . The document opens when I shellexecute but no text is pasted.Please help.

Opt("ExpandVarStrings", 1)

Dim $sTxt = FileRead(@ScriptDir & '\"D:\Objects To Burn\Documents\iGoogle.txt"')

Dim $sFirst1 = 'Location: United States'
Dim $sFirst2 = 'View full reportGoogle Insights for SearchRestore'
Dim $sSecond1 = 'Category: Automotive'
Dim $sSecond2 = 'View full report'
Dim $sPatt = '(?s)(? :( \Q$sFirst1$\E)|(\Q$sSecond1$\E))(.*?)((?1)\Q$sFirst2$\E|\Q$sSecond2$\E)'

Dim $avMatch = StringRegExp($sTxt, $sPatt, 3)
If IsArray($avMatch) Then
    #cs
    For $i = 0 To UBound($avMatch)-1
        ConsoleWrite($avMatch[$i] & @LF)
    Next
    #ce
    
    For $i = 0 To UBound($avMatch)-3 Step 3
        ConsoleWrite($avMatch[$i+2] & @LF & @LF)
    Next
EndIf

shellexecute("D:\Documents and Settings\Taevon Jones\Desktop\New Text document.txtNew Text Document")
Send("{CTRLDOWN}v{CTRLUP}")
Edited by styles3000
Link to comment
Share on other sites

You didnt open the file for reading. You must use Fileopen() before you can use Fileread(). Nevermind what I just said.

The file read part is wrong though.

Dim $sTxt = FileRead(@ScriptDir & '\"D:\Objects To Burn\Documents\iGoogle.txt"')
Are you sure you want @scriptDir to be there? Edited by bchris01
Link to comment
Share on other sites

Look at the changes I've made. @ScriptDir for example evaluated to the path the file have been executed from, so it won't be D:\path\D:\Objects To Burn\Documents\iGoogle.txt for sure. You may want to read the AutoIt 1-2-3 topic in the Example Forum because you're missing the basics.

Opt("ExpandVarStrings", 1)

Dim Const $sFile = "D:\Documents and Settings\Taevon Jones\Desktop\New Text document.txt"

Dim $sTxt = FileRead("D:\Objects To Burn\Documents\iGoogle.txt")
Dim $hFile = FileOpen($sFile, 2) ; 2 Erase previous content. 1 Append to the content.

Dim $sFirst1 = 'Location: United States'
Dim $sFirst2 = 'View full reportGoogle Insights for SearchRestore'
Dim $sSecond1 = 'Category: Automotive'
Dim $sSecond2 = 'View full report'
Dim $sPatt = '(?s)(? :( \Q$sFirst1$\E)|(\Q$sSecond1$\E))(.*?)((?1)\Q$sFirst2$\E|\Q$sSecond2$\E)'

Dim $avMatch = StringRegExp($sTxt, $sPatt, 3)
If IsArray($avMatch) Then
    For $i = 0 To UBound($avMatch)-4 Step 4
        FileWrite($hFile, $avMatch[$i+2])
    Next
EndIf

FileClose($hFile)
ShellExecute($sFile)
Edited by Authenticity
Link to comment
Share on other sites

You didnt open the file for reading. You must use Fileopen() before you can use Fileread(). Nevermind what I just said.

The file read part is wrong though.

Dim $sTxt = FileRead(@ScriptDir & '\"D:\Objects To Burn\Documents\iGoogle.txt"')
Are you sure you want @scriptDir to be there?

Bchris,

I'm a not sure I want the @scriptDir or not because I don't know what exactly it does. I'm a novice. I might not need it because the script given to me, opened the document but still doesn't output the text that was extracted. I'm so close , the script is beautiful, but could I invite someone to connect with me remotely(Teamview) so I can show someone first hand what the script is doing and not doing, and what I'm trying do.

Link to comment
Share on other sites

How should the Fileread part be?

Dim $sTxt = FileRead("D:\Objects To Burn\Documents\iGoogle.txt"')

Also best to get rid of Dim and instead declare the variable in the proper scope, Local or Global.

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

Bchris,

I'm a not sure I want the @scriptDir or not because I don't know what exactly it does. I'm a novice. I might not need it because the script given to me, opened the document but still doesn't output the text that was extracted. I'm so close , the script is beautiful, but could I invite someone to connect with me remotely(Teamview) so I can show someone first hand what the script is doing and not doing, and what I'm trying do.

@scriptdir is the location that your script is running from. So if iGoogle.txt was located in the same directory as your script you could use:

$sTxt = Fileread(@scriptdir & '\iGoogle.txt')
to read the txt file.
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...