Jump to content
Sign in to follow this  
crazyjts

Could use some help with _StringInStr and _StingBetween

Recommended Posts

crazyjts

I'm trying to search a file for a given string. Once I have that string, I want to get a users name out of it. Then search again for a new string including that users name. Once I have the second string, I want to get their reply out of it.

The section of code below looks right to me but it doesn't seem to find what I'm searching for nor does it return the _StringBetween values. I know the file has the info I'm looking for.

The file is of unknown and varying length but should have only one instance of each thing I am searching for.

I appreciate any help on this. Thanks

$File = FileOpen ("C:\testfile.txt",0)
$LineCount = _FileCountLines($File)
For $Line = 1 To $LineCount
    If StringInStr(FileReadLine($File, $Line), "Attention ") Then
        $RqstTo = _StringBetween(FileReadLine($File, $Line),"Attention ",", please forward the information.") 
    EndIf
Next
For $line = 1 To $LineCount
    If StringInStr(FileReadLine($File, $line), $RdstTo & " replies, ") Then
        $Rspns = _StringBetween(FileReadLine($File, $line),$RqstTo & "'s reply, ",".")
    EndIf
Next

The TestFile looks like:

This is a test file
This is line two of the file
Attention Mike, please forward the information.
This is line four of the file
Mike's reply, Right away.

The results I am looking for are:

$RqstTo = "Mike" (from line 3)

$Rspns = "Right away" (from line 5)

absolution (Tault)

Edited by crazyjts

Share this post


Link to post
Share on other sites
crazyjts

I just ran a test and for some reason the $LineCount value = 0 even though there are 5 lines in the TestFile.txt file.

I think this may be the issue and not my If Then loops.

Anyone see what I'm doing wrong with this?

Edited by crazyjts

Share this post


Link to post
Share on other sites
dantay9

_StringBetween returns an array. Instead of reading each line separately, you can use fileread. Try this.

#include <Array.au3>
#include <String.au3>
$File = FileRead("C:\testfile.txt")
$Line3 = _StringBetween($File, "Attention ", ", please")
$Line5 = _StringBetween($File, "Mike's reply, ", ".")
_ArrayDisplay($Line3)
_ArrayDisplay($Line5)

[font="Verdana"] [size="2"]"[/size][/font]Failure is not an option -- it comes packaged with Windows"[font="Verdana"][size="2"] Gecko Web Browser[/size][/font][font="Verdana"][size="2"], [/size][/font][font="Verdana"][size="2"]Yahtzee![/size][/font][font="Verdana"][size="2"], Toolbar Launcher (like RocketDock)[/size][/font][font="Verdana"][size="2"]Internet Blocker, Simple Calculator, Local Weather, Easy GDI+ GUI [/size][/font][font="Verdana"][size="2"]Triangle Solver, TCP File Transfer, [/size][/font][font="Verdana"][size="2"]Valuater's Autoit Wrappers[/size][/font][font="Verdana"][size="3"][size="2"][size="2"]OOP In AutoIt[/size][/size][/size][/font][font="Verdana"][size="2"][size="1"]Using Windows XP SP3, 1GB RAM, AMD Athlon Processor @ 2.1 GHzCheck me out at gadgets.freehostrocket.com[/size][/size][/font]

Share this post


Link to post
Share on other sites
crazyjts

_StringBetween returns an array. Instead of reading each line separately, you can use fileread. Try this.

#include <Array.au3>
#include <String.au3>
$File = FileRead("C:\testfile.txt")
$Line3 = _StringBetween($File, "Attention ", ", please")
$Line5 = _StringBetween($File, "Mike's reply, ", ".")
_ArrayDisplay($Line3)
_ArrayDisplay($Line5)
I'm not up to snuff on arrays so maybe _StringBetween isn't what I should use.

Is there an "easy" way to find a string within a sting assuming I know the beginning and end of the string?

Let's say my string is "Attention Mike, please forward the information."

Can I somehow search for the string containing "Attention " and then find the string that sits between "Attention " and ", please forward the information." and then use that found string in another search?

Share this post


Link to post
Share on other sites
Varian

Maybe with RegExps:

$File = FileRead("C:\testfile.txt")
$RqstTo = StringRegExpReplace($File, '[[:ascii:]]*Attention (.*), please forward the information\.[[:ascii:]]*', "$1")
$Rspns = StringRegExpReplace($File, "[[:ascii:]]*" & $RqstTo & "'s reply, (.+)\.[[:ascii:]]*", "$1")
If $Rspns Then msgbox(32, 'Response', $Rspns)

Share this post


Link to post
Share on other sites
crazyjts

_StringBetween returns an array. Instead of reading each line separately, you can use fileread. Try this.

#include <Array.au3>
#include <String.au3>
$File = FileRead("C:\testfile.txt")
$Line3 = _StringBetween($File, "Attention ", ", please")
$Line5 = _StringBetween($File, "Mike's reply, ", ".")
_ArrayDisplay($Line3)
_ArrayDisplay($Line5)
I spent some time tonight and figured this out. Thank you Dantay.

Share this post


Link to post
Share on other sites
crazyjts

I spent some time tonight and figured this out. Thank you Dantay.

Ok so I didn't get it as well as I thought I did. I've got 2 problems:

1) I cannot get the $Rqst = _StringBetween($File, $Who & "tells you, '","'.") to work

2) I cannot figure out how to use $Who & $Rqst in a message box.

The array display shows the values as I expect but when I write up a basic msgbox none of the text shows up. I guess it comes down to that fact that I don't understand how to use the array values in another operation.

Any help is appreciated.

#Include <Misc.au3>
#Include <File.au3>
#include <Array.au3>
#include <String.au3>

$File = FileRead("C:\log.txt")
$Who = _StringBetween($File, "You ask ",", 'What would you like?'")
$Rqst = _StringBetween($File, $Who & " tells you, '","'.")
_ArrayDisplay($Who, "Name")
_ArrayDisplay($Rqst, "Request")
MsgBox(0,"Info",$Who & " wants " & $Rqst)

Log.txt file

Logging ON
Current log file size: 0 KB
You ask Elvis, 'What would you like?'.
Elvis tells you, 'Fried banana and peanut butter sandwich'.

Share this post


Link to post
Share on other sites
Varian

If you use _StringBetween it returns an array, so if it finds a match you'll have to specify an element to display. You should use:

#Include <Misc.au3>
#Include <File.au3>
#include <Array.au3>
#include <String.au3>

$File = FileRead("C:\log.txt")
$Who = _StringBetween($File, "You ask ",", 'What would you like?'")
$Rqst = _StringBetween($File, $Who[0] & " tells you, '","'.")
_ArrayDisplay($Who, "Name")
_ArrayDisplay($Rqst, "Request")
MsgBox(0,"Info",$Who[0] & " wants " & $Rqst[0])oÝ÷ ØZ+­Ûzk2¢æ«yÚ"¢}ý¶ØbKajܨºwu©Ú®¶²¶Þ¶êç²)ÜzØ^­íý²åÈãD«'ç¢ÛayƬz('¢·¢YhÂ)àbjw[aÇîËb¢y®r²'«jwb¶·­º¹ìjËkx'¢Ö§jºÚÉ(§qè­j^j»-«r¢æ«zZ()à~ÿ«Þ¢yrÖf­rh"w%¹×¬ç²Æ«É«­¢+ØÀÌØí¥±ô¥±I ÅÕ½ÐíèÀäÈí±½¹ÑáÐÅÕ½Ðì¤((ÀÌØí]¡¼ô}MÑÉ¥¹ Ñݹà ÀÌØí¥±°ÅÕ½Ðíe½ÔͬÅÕ½Ðì°ÅÕ½Ðì°Ìäí]¡Ðݽձå½Ô±¥­üÌäìÅÕ½Ðì¤)%ÀÌØí]¡¼Q¡¸ÀÌØíIÅÍÐô}MÑÉ¥¹    Ñݹà ÀÌØí¥±°ÀÌØí]¡¼µÀìÅÕ½Ðìѱ±Ìå½Ô°ÌäìÅÕ½Ðì°ÅÕ½ÐìÌäì¸ÅÕ½Ðì¤)5Í    ½à À°ÅÕ½Ðí%¹¼ÅÕ½Ðì°ÀÌØí]¡¼µÀìÅÕ½ÐìݹÑÌÅÕ½ÐìµÀìÀÌØíIÅÍФ()Õ¹}MÑÉ¥¹  Ñݹà ÀÌØíMÑÉ¥¹°ÀÌØíMÑÉаÀÌØí¹¤(IÑÕɸMÑÉ¥¹IáÁIÁ± ÀÌØíMÑÉ¥¹°ÅÕ½Ðì ý¤¥mléÍ¥¤éut¨ÀäÈíDÅÕ½ÐìµÀìÀÌØíMÑÉеÀìÅÕ½ÐìÀäÈí ¸¨¤ÀäÈíDÅÕ½ÐìµÀìÀÌØí¹µÀìÅÕ½ÐìÀäÈímléÍ¥¤éut¨ÅÕ½Ðì°ÅÕ½ÐìÀÌØìÄÅÕ½Ðì¤)¹Õ¹ìôôÐí}MÑÉ¥¹    Ñݹà
Edited by Varian

Share this post


Link to post
Share on other sites
crazyjts

I tried something very similar to this thinking this was the right approach. I get the same error with your code and with mine.

C:\Users\Name\Desktop\Array Test.au3 (16) : ==> Subscript used with non-Array variable.:

$Rqst = _StringBetween($File, $Who[0] & " tells you, '","'.")

$Rqst = _StringBetween($File, $Who^ ERROR

It seems not to recognize the [0] position which seems to me to be the only position in the array since it is only looking for one thing.

Share this post


Link to post
Share on other sites
Varian

When you do _arraydisplay, what does the index show..I got only the [0] index returned

Share this post


Link to post
Share on other sites
crazyjts

Odd...I don't know what I did wrong...must've been a copy/paste error. I copied the code again and it's working perfectly. Thank you Varian.

Share this post


Link to post
Share on other sites
crazyjts

Ugh...one more problem I didn't foresee.

I have an instance where the string "You ask Elvis 'What would you like?'" does not exist in the file. I believe the _StringBetween array for $Who is coming back empty and causing an error. How do I check if the line exists or not prior to using the $Who = _StringBetween($File, "You ask ",", 'What would you like?'") function?

Edited by crazyjts

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.