Sign in to follow this  
Followers 0
Dracil

Finding Text in txt, then looking under it.

6 posts in this topic

Ok i have a scripts that scans my txt file for a "text, in this example its gregor,

$file = FileOpen("folderlist.txt", 0)
$read = FileRead($file)
If @error = -1 Then
    MsgBox(0, "Error", "File not read")
    Exit
Else

    If StringRegExp($read, "gregor") Then
        MsgBox(0, "Oops", "Match")
    Else
        MsgBox(0, "Oops", "No match")
    EndIf
EndIf
FileClose($file)

Now what i would like to do is, IF and WHEN i find "gregor" in the txt file i would like to grab everything between it and for example "gregor" & "end", so "gregorend"

So lets say it looks like this in the txt file:

gregor

Hello my name

Is Gregor

gregorend

How do i grab "

Hello my name

Is Gregor"

Ive tried the help file and searched the forum but i cant fint it =/

I would really appreaciate help with this!

Thanks in advance

Share this post


Link to post
Share on other sites



$sTxt = "gregor" & @CRLF
$sTxt &= "Hello, my name" & @CRLF
$sTxt &= "is Gregor." & @CRLF
$sTxt &= "gregorend"
$aSRE = StringRegExp($sTxt, "(?i)gregor\s*([\w\s,]+[.?!]*)\s*gregorend", 1)
If NOT @Error Then
   MsgBox(0, "Result", StringStripWS($aSRE[0], 3))
EndIf


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!"

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Thanks!, Ok it looks like this now:

$file = FileOpen("folderlist.txt", 0)
$read = FileRead($file)
If @error = -1 Then
    MsgBox(0, "Error", "File not read")
    Exit
Else


    If StringRegExp($read,"gregor") Then

$aSRE = StringRegExp($read, "(?i)gregor\s*([\w\s,]+[.?!]*)\s*gregorend", 1)
        MsgBox(0, "Yes", StringStripWS($aSRE[0], 3))


    Else
        MsgBox(0, "Oops", "No match")
    EndIf
EndIf
FileClose($file)

And it works great!, BUT!

As long a the text in between gregor and gregorend is a maximum of 3 rows, if its more it wont work, why is that and how can i change that?

Thanks!

Edited by Dracil

Share this post


Link to post
Share on other sites

Try this.

#include <String.au3>

$read = "So lets say it looks like this in the txt file: gregor Hello my name" & @CRLF & _
        "Is Gregor" & @CRLF & _
        "gregorend" & @CRLF & _
        "How do i grab " ; FileRead($file)

If StringInStr($read, "gregor") And StringInStr($read, "gregorend") Then
    MsgBox(0, "In Between", StringRegExpReplace($read, "(?s)(?:.*gregor\s)(.*)(?:\sgregorend.*)", "\1"))
Else
    MsgBox(0, "Oops", "No match")
EndIf

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

That works great ! thanks! What if i want the $read should include data from a website? And the whole website lets say www.google.se ? And that without having to actually open the browser.

Edited by Dracil

Share this post


Link to post
Share on other sites

See InetRead() and BinaryToString()


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!"

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