Jump to content
Sign in to follow this  

StringBetween or _sre

Recommended Posts


Hello Everyone,

"Long time listener, first time caller"

Anyway, Ive written a few codes from various things so I can say I understand some of whats going on. I know I should have some code to be posting for my question but atm Im at work (very slow now) and I dont have that information available.

My question is this: I have a program now that outputs this .txt doc

1. Skill Name (xx days, xx hours, xx min, xx sec)

2. ....

And I cant for the life of me figure out the Stringbetween code, I cant remember the name of the other type of code but it is /*?days/* etc and I dont know how to write it, even from searching the forums and seeing it in action.

I need to be able to save the xx information as separate vars and to also check the line number 1., 2. etc.

The second Ive gotten to work but it returns all lines with 1. In them IE 11. , 111. . And I just need it do the one Im looking for.



Share this post

Link to post
Share on other sites

Here is the info that i didnt have at work.

1. Eidetic Memory II (2 hours, 55 minutes, 36 seconds)
2. Iron Will I (11 minutes, 57 seconds)
3. Iron Will II (55 minutes, 42 seconds)
4. Industry III (5 hours, 14 minutes, 55 seconds)
5. Industry IV (1 day, 5 hours, 41 minutes, 41 seconds)
6. Industry V (6 days, 23 hours, 58 minutes, 40 seconds)
7. Science IV (1 day, 2 hours, 1 minute, 27 seconds)
8. Astrogeology I (31 minutes, 58 seconds)
9. Astrogeology II (2 hours, 28 minutes, 53 seconds)
10. Astrogeology III (14 hours, 2 minutes, 9 seconds)
11. Mining Barge I (1 hour, 1 minute, 16 seconds)
12. Mining Barge II (4 hours, 45 minutes, 21 seconds)
13. Mining Barge III (1 day, 2 hours, 54 minutes, 9 seconds)
14. Astrogeology IV (3 days, 7 hours, 24 minutes, 3 seconds)

#include <String.au3>
#include <array.au3>
#include <file.au3>

$sText = FileRead ("test.txt")
$file = FileOpen("test.txt", 0)

; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
Dim $array
; Read in lines of text until the EOF is reached
While 1
    $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
    If StringInStr($line, "1.") Then
        $found = 0
        For $x = 0 To UBound($array) - 1
            If $line == $array[$x] Then
                $found = 1
        If Not $found Then
            If IsArray($array) Then
                ReDim $array[UBound($array) + 1]
                Dim $array[1]
            $array[UBound($array) - 1] = $line
For $x = 0 To UBound($array) - 1
    $array = _StringBetween($sText,"(", ")")
    $test = $array[$x]
    MsgBox(0, "Body Text", $test)

When I run this code i get 2 boxes.

First one is 2 hours, 55 minutes, 36 seconds

Second one is 11 minutes, 57 seconds

Im getting closer to what I was looking for, but i seem to be doing something wrong.

Share this post

Link to post
Share on other sites


Share this post

Link to post
Share on other sites

First, _FileReadToArray might save you a lot of work. Saves you from reinventing the wheel and prevents any accidental errors you might make.

Then loop the returned array to search for the thing you are looking for.

Furthermore, StringInStr("111. blabla","1.") will trigger, so this is probably not what you want, like you described in your first post. Try something like: (if I undertand you correctly)

If StringLeft($line,StringInStr($line,".")) = "11." then ; <- Will NOT trigger on "1. blabla" nor on "111. blabla"

/edit: error in code

Edited by SadBunny

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share this post

Link to post
Share on other sites

Interesting. So, you basically just want all the numbers, right?

Give this code a whirl.

#include <File.au3>

Global $aFileContents
_FileReadToArray(@ScriptDir & '\example.txt', $aFileContents)
If @error Then
    MsgBox(16, 'Error', 'Error opening file.')

For $i = 1 to $aFileContents[0]
    $aMatches = StringRegExp($aFileContents[$i], '([0-9]+)', 3)
    If Not @error Then
        If UBound($aMatches) = 4 Then
            MsgBox(48, 'From file line ' & $i, 'Index: ' & $aMatches[0] & @LF & _
                'Hours: ' & $aMatches[1] & @LF & _
                'Minutes: ' & $aMatches[2] & @LF & _
                'Seconds: ' & $aMatches[3])
        ElseIf UBound($aMatches) = 5 Then
            MsgBox(48, 'From file line ' & $i, 'Index: ' & $aMatches[0] & @LF & _
                'Days: ' & $aMatches[1] & @LF & _
                'Hours: ' & $aMatches[2] & @LF & _
                'Minutes: ' & $aMatches[3] & @LF & _
                'Seconds: ' & $aMatches[4])

This assumes that the text file is saved as example.txt and is in the same directory you run the script from.

*Edit: Removed extraneous code.

Edited by Saunders

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