Jump to content

Any tutorial showing examples of StringRegExp?


Recommended Posts

Having troubles figuring out how it works. For example having problems with these lines:

$as_monstermiss = StringRegExp($s_File, '( .*?) attacks you and misses!', 3)

=> (this is how the line looks): [23:30:24]aimless boogey attacks you and misses!

When i parse it with StringRegExp it stops and program haveto be shut down with force.

I want the monster name out of the string, the monster name will vary alot.

;$as_monsterattacksyou = StringRegExp($s_File, ' ( .*) hits your', 3)

=> (this is how the line looks): [23:30:28] aimless boogey hits your foot for 38 (-13) damage!

Same as above, when StringRegExp parse the log it stops and haveto be shut down with force.

In this line i want the "foot" part out, this change, foot, legs, torso, head etc.

;$as_monsterdead = StringRegExp($s_File, '(.*?) dies!', 3)

=> (this is how the line looks): [23:30:42] aimless boogey dies!

Im trying to extract the name of the monster in this line, as the examples above.

This line also changes depending on what monster you are fighting.

Maybe it haveto to with having the (.*?) in front of a string, rather then in the middle?

"I'm paper, rock is fine, nerf scissors!!!"

Link to comment
Share on other sites

  • Moderators

Not so easy anymore? I nominate w0uter to write it!

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

$as_monstermiss = StringRegExp($s_File, '( .*?) attacks you and misses!', 3)

=> (this is how the line looks): [23:30:24]aimless boogey attacks you and misses!

When i parse it with StringRegExp it stops and program haveto be shut down with force.

I want the monster name out of the string, the monster name will vary alot.

This works for me.

;$as_monsterattacksyou = StringRegExp($s_File, ' ( .*) hits your', 3)

=> (this is how the line looks): [23:30:28]aimless boogey hits your foot for 38 (-13) damage!

Same as above, when StringRegExp parse the log it stops and haveto be shut down with force.

In this line i want the "foot" part out, this change, foot, legs, torso, head etc.

Try this instead (works for me).

StringRegExp($s_File, "hits your (.*?) for",3)

;$as_monsterdead = StringRegExp($s_File, '(.*?) dies!', 3)

=> (this is how the line looks): [23:30:42]aimless boogey dies!

Im trying to extract the name of the monster in this line, as the examples above.

This line also changes depending on what monster you are fighting.

Try this instead (works for me). Notice the extra space.

StringRegExp($s_File, "( .*?) dies!",3)

These all return an array with 1 dimension, so make sure that you are using $as_monstermiss[0] or $as_monstermiss[1] (can't remember if the first index is 0 or 1!) instead of $as_monstermiss.

“Give a man a script; you have helped him for today. Teach a man to script; and you will not have to hear him whine for help.”AutoIt4UE - Custom AutoIt toolbar and wordfile for UltraEdit/UEStudio users.AutoIt Graphical Debugger - A graphical debugger for AutoIt.SimMetrics COM Wrapper - Calculate string similarity.

Link to comment
Share on other sites

lol smoke :o

Its a tricky part this StringRegExp but its really simple if one knows how it works. Alot of triggers etc, for me i would prefare to see them in examples with different settings etc. It seems that there are problems using brackets and starting with a (.*?) before the rest of the string.

"I'm paper, rock is fine, nerf scissors!!!"

Link to comment
Share on other sites

$as_monstermiss = StringRegExp($s_File, '( .*?) attacks you and misses!', 3)
$as_monstermiss[0] = UBound($as_monstermiss)
MsgBox(0, "", "Monster missed you: " & $as_monstermiss[0])

Hehe i dont know how you did this, but i cant get it to work at all!

What happens is that the script seems to "hang" up, so im forced to shut it down by force.

<Edit> wooo... ok, you need the "full" log to get the problem! (it worked with 1 line, but not with the log> </edit>

Edited by huldu

"I'm paper, rock is fine, nerf scissors!!!"

Link to comment
Share on other sites

lol smoke :o

Its a tricky part this StringRegExp but its really simple if one knows how it works. Alot of triggers etc, for me i would prefare to see them in examples with different settings etc. It seems that there are problems using brackets and starting with a (.*?) before the rest of the string.

I have been doing alot with RegEx lately scraping web pages and the help file is confusing. You are better off searching the web as there are many reg ex web sites. I find that the best method is trial and error. I use the following reg ex tester that I am writing/wrote to find the reg ex that I need. Saves alot of time.

Here it is:

CODE
#Include <GUIListView.au3>

#include <GUIConstants.au3>

; == GUI generated with Koda ==

$MainForm = GUICreate("RegEx", 634, 432, 879, 118)

$MainForm_String = GUICtrlCreateEdit("", 8, 8, 617, 129)

GUICtrlSetData($MainForm_String, "sfasf<asd>asdf"&@CRLF&"sdf<saddffff>dfdf")

GUICtrlCreateLabel("Regular &Expression:", 8, 152, 104, 17)

$MainForm_RegExp = GUICtrlCreateInput("<(.*)>", 112, 144, 505, 21)

$MainForm_Check = GUICtrlCreateButton("Check", 472, 176, 73, 25, $BS_DEFPUSHBUTTON)

$MainForm_Reset = GUICtrlCreateButton("Reset", 552, 176, 73, 25, 0)

$lblFlagSetting0Result = GUICtrlCreateLabel("lblFlagSetting0Result", 8, 248, 103, 17)

GUICtrlCreateLabel("Flag Setting: 0 or 2", 8, 216, 93, 17)

GUICtrlCreateLabel("Flag Setting 1:", 224, 216, 72, 17)

GUICtrlCreateLabel("Flag Setting 3:", 424, 216, 72, 17)

$lblFlagSetting0Error = GUICtrlCreateLabel("lblFlagSetting0Error", 8, 384, 199, 17)

$lblFlagSetting2Error = GUICtrlCreateLabel("lblFlagSetting2Error", 216, 384, 199, 17)

$lblFlagSetting3Error = GUICtrlCreateLabel("lblFlagSetting3Error", 424, 384, 199, 17)

$lblFlagSetting0Extended = GUICtrlCreateLabel("lblFlagSetting0Extended", 8, 408, 198, 17)

$lblFlagSetting2Extended = GUICtrlCreateLabel("lblFlagSetting2Extended", 216, 408, 198, 17)

$lblFlagSetting3Extended = GUICtrlCreateLabel("lblFlagSetting3Extended", 424, 408, 198, 17)

$lvwFlagSetting2 = GUICtrlCreateListView("No.|Value", 208, 248, 209, 121)

$lvwFlagSetting3 = GUICtrlCreateListView("No.|Value", 424, 248, 201, 121)

;~ _GUICtrlListViewJustifyColumn(-1, 0, 2)

_GUICtrlListViewSetColumnWidth (-1, 0, 33)

_GUICtrlListViewSetColumnWidth (-1, 1, 215)

GUISetState()

Dim $MatchedResult, $MatchedAmount, $String, $RegExp, $CheckType, $AboutDialog = 0, $About_ButtonOK

Dim $StatusDefault = GUICtrlRead($lblFlagSetting0Result)

While 1

$msg = GUIGetMsg()

Select

Case $msg = $GUI_EVENT_CLOSE

ExitLoop

Case $msg = $MainForm_Reset

GUICtrlSetData($MainForm_String, "")

GUICtrlSetData($MainForm_RegExp, "")

GUICtrlSetState($MainForm_String, $GUI_FOCUS)

Case $msg = $MainForm_Check

_GUICtrlListViewDeleteAllItems ($lvwFlagSetting2)

_GUICtrlListViewDeleteAllItems ($lvwFlagSetting3)

$String = GUICtrlRead($MainForm_String)

$RegExp = GUICtrlRead($MainForm_RegExp)

Select

Case $String = ""

GUICtrlSetState($MainForm_String, $GUI_FOCUS)

Case $RegExp = ""

GUICtrlSetState($MainForm_RegExp, $GUI_FOCUS)

Case Else

$MatchedResult = StringRegExp($String, $RegExp, 0)

$Error = @error

$Extended = @extended

GUICtrlSetData($lblFlagSetting0Result, "Result: " & $MatchedResult)

Select

Case $Error = 0

GUICtrlSetData($lblFlagSetting0Error, "@Error: Executed properly.")

Case $Error = 1

GUICtrlSetData($lblFlagSetting0Error, "@Error: Flag invalid.")

Case $Error = 2

GUICtrlSetData($lblFlagSetting0Error, "@Error: Pattern invalid.")

EndSelect

Select

Case $Extended = 0

GUICtrlSetData($lblFlagSetting0Extended, "@Extended: Result 0.")

Case $Extended = 1

GUICtrlSetData($lblFlagSetting0Extended, "@Extended: Result 1.")

EndSelect

$MatchedResult = StringRegExp($String, $RegExp, 1)

$Error = @error

$Extended = @extended

$MatchedAmount = UBound($MatchedResult)

For $i = 0 To $MatchedAmount - 1

GUICtrlCreateListViewItem(($i) & "|" & $MatchedResult[$i], $lvwFlagSetting2)

Next

Select

Case $Error = 0

GUICtrlSetData($lblFlagSetting2Error, "@Error: Executed properly.")

Case $Error = 1

GUICtrlSetData($lblFlagSetting2Error, "@Error: Flag invalid.")

Case $Error = 2

GUICtrlSetData($lblFlagSetting2Error, "@Error: Pattern invalid.")

EndSelect

Select

Case $Extended = 0

GUICtrlSetData($lblFlagSetting2Extended, "@Extended: Match not found.")

Case $Extended = 1

GUICtrlSetData($lblFlagSetting2Extended, "@Extended: Match found.")

EndSelect

$MatchedResult = StringRegExp($String, $RegExp, 3)

$Error = @error

$Extended = @extended

$MatchedAmount = UBound($MatchedResult)

Select

Case $Error = 0

GUICtrlSetData($lblFlagSetting3Error, "@Error: Executed properly.")

Case $Error = 1

GUICtrlSetData($lblFlagSetting3Error, "@Error: Flag invalid.")

Case $Error = 2

GUICtrlSetData($lblFlagSetting3Error, "@Error: Pattern invalid.")

EndSelect

Select

Case $Extended = 0

GUICtrlSetData($lblFlagSetting3Extended, "@Extended: Match not found.")

Case $Extended = 1

GUICtrlSetData($lblFlagSetting3Extended, "@Extended: Match found.")

EndSelect

For $i = 0 To $MatchedAmount - 1

GUICtrlCreateListViewItem(($i) & "|" & $MatchedResult[$i], $lvwFlagSetting3)

Next

EndSelect

EndSelect

WEnd

“Give a man a script; you have helped him for today. Teach a man to script; and you will not have to hear him whine for help.”AutoIt4UE - Custom AutoIt toolbar and wordfile for UltraEdit/UEStudio users.AutoIt Graphical Debugger - A graphical debugger for AutoIt.SimMetrics COM Wrapper - Calculate string similarity.

Link to comment
Share on other sites

$as_monstermiss = StringRegExp($s_File, '( .*?) attacks you and misses!', 3)
$as_monstermiss[0] = UBound($as_monstermiss)
MsgBox(0, "", "Monster missed you: " & $as_monstermiss[0])

Hehe i dont know how you did this, but i cant get it to work at all!

What happens is that the script seems to "hang" up, so im forced to shut it down by force.

Make sure that you have the latest beta and try the reg ex in the program I posted.

“Give a man a script; you have helped him for today. Teach a man to script; and you will not have to hear him whine for help.”AutoIt4UE - Custom AutoIt toolbar and wordfile for UltraEdit/UEStudio users.AutoIt Graphical Debugger - A graphical debugger for AutoIt.SimMetrics COM Wrapper - Calculate string similarity.

Link to comment
Share on other sites

$as_monstermiss = StringRegExp($s_File, '( .*?) attacks you and misses!', 3)
$as_monstermiss[0] = UBound($as_monstermiss)
MsgBox(0, "", "Monster missed you: " & $as_monstermiss[0])

Hehe i dont know how you did this, but i cant get it to work at all!

What happens is that the script seems to "hang" up, so im forced to shut it down by force.

<Edit> wooo... ok, you need the "full" log to get the problem! (it worked with 1 line, but not with the log> </edit>

I assume that you are parsing the file line by line, not the complete file text?

“Give a man a script; you have helped him for today. Teach a man to script; and you will not have to hear him whine for help.”AutoIt4UE - Custom AutoIt toolbar and wordfile for UltraEdit/UEStudio users.AutoIt Graphical Debugger - A graphical debugger for AutoIt.SimMetrics COM Wrapper - Calculate string similarity.

Link to comment
Share on other sites

Downloading the latest beta and trying (think im using the autoit-v3.1.1.106-beta-Setup.exe). I had problems with 107 beta and GUI refresh.

The entire log, basicly using those lines. If there is a better way please show me :o

<Edit> I got same problem with .110 beta. The parsing seems to either freeze or lock up, in any way i haveto force quit the program. This only happens with a couple of lines, the rest works fine. </edit>

Edited by huldu

"I'm paper, rock is fine, nerf scissors!!!"

Link to comment
Share on other sites

This works, may be what you want.

$file = FileOpen(@ScriptDir & "\chat.log", 0)

; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

; Read in lines of text until the EOF is reached
While 1
   $line = FileReadLine($file)
   If @error = -1 Then ExitLoop
    If StringRegExp($line, '( .*?) attacks you and misses!', 0) Then
        $as_monstermiss = StringRegExp($line, '( .*?) attacks you and misses!', 3)
        MsgBox(0,"","Attacked by: " & $as_monstermiss[0])
    EndIf
    If StringRegExp($line, "hits your (.*?) for",0) Then
        $as_monsterattacksyou = StringRegExp($line, "hits your (.*?) for",3)
        MsgBox(0,"","He hit my: " & $as_monsterattacksyou[0])
    EndIf
    If StringRegExp($line, "( .*?) dies!",0) Then
        $as_monsterdead = StringRegExp($line, "( .*?) dies!",3)
        MsgBox(0,"","I killed: " & $as_monsterdead[0])
    EndIf
    
   ;MsgBox(0, "Line read:", $line)
Wend

FileClose($file)

“Give a man a script; you have helped him for today. Teach a man to script; and you will not have to hear him whine for help.”AutoIt4UE - Custom AutoIt toolbar and wordfile for UltraEdit/UEStudio users.AutoIt Graphical Debugger - A graphical debugger for AutoIt.SimMetrics COM Wrapper - Calculate string similarity.

Link to comment
Share on other sites

When i remove the MsgBoxes from inside the loop, the script "freeze" or whatever happens, why is this?. I also tried to build up a "total" using the: $as_monstermiss[0] = UBound($as_monstermiss), when i call it in a msgbox outside the loop, to show how many times a monster missed you same as above happens.

"I'm paper, rock is fine, nerf scissors!!!"

Link to comment
Share on other sites

$file = FileOpen(@ScriptDir & "\chat.log", 0)

$MonsterMissed = 0
$MonsterAttacked = 0
$MonsterDied = 0
; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

; Read in lines of text until the EOF is reached
While 1
   $line = FileReadLine($file)
   If @error = -1 Then ExitLoop
    If StringRegExp($line, '( .*?) attacks you and misses!', 0) Then
        $as_monstermiss = StringRegExp($line, '( .*?) attacks you and misses!', 3)
       ;MsgBox(0,"","Attacked by: " & $as_monstermiss[0])
        $MonsterMissed = $MonsterMissed + 1
    EndIf
    If StringRegExp($line, "hits your (.*?) for",0) Then
        $as_monsterattacksyou = StringRegExp($line, "hits your (.*?) for",3)
      ;MsgBox(0,"","He hit my: " & $as_monsterattacksyou[0])
       $MonsterAttacked = $MonsterAttacked + 1
    EndIf
    If StringRegExp($line, "( .*?) dies!",0) Then
        $as_monsterdead = StringRegExp($line, "( .*?) dies!",3)
      ;MsgBox(0,"","I killed: " & $as_monsterdead[0])
       $MonsterDied = $MonsterDied + 1
    EndIf
    
  ;MsgBox(0, "Line read:", $line)
Wend

MsgBox(0, "", "Stats: " & $MonsterMissed & ":" & $MonsterAttacked & ":" & $MonsterDied)

FileClose($file)

Why wont this work, is there something with the StringRegExp i should know about? :o

"I'm paper, rock is fine, nerf scissors!!!"

Link to comment
Share on other sites

Works for me. Takes about 30 secs on a P4 2.8GHz machine.

Are you waiting long enough?

“Give a man a script; you have helped him for today. Teach a man to script; and you will not have to hear him whine for help.”AutoIt4UE - Custom AutoIt toolbar and wordfile for UltraEdit/UEStudio users.AutoIt Graphical Debugger - A graphical debugger for AutoIt.SimMetrics COM Wrapper - Calculate string similarity.

Link to comment
Share on other sites

Oh... im feeling more stupid then normally, i assumed the "process" would only take a few seconds at most. Other lines parse really fast, thats what made me think the script crashed!

Is there any way to make a progress bar for this to get an idea when the script is done going thru the log?

Thank you once again!

Edited by huldu

"I'm paper, rock is fine, nerf scissors!!!"

Link to comment
Share on other sites

Oh... im feeling more stupid then normally, i assumed the "process" would only take a few seconds at most. Other lines parse really fast, thats what made me think the script crashed!

Is there any way to make a progress bar for this to get an idea when the script is done going thru the log?

Thank you once again!

Remember you only have one wish left!

CODE
#include <File.au3>

$file = FileOpen(@ScriptDir & "\chat.log", 0)

#include <GUIConstants.au3>

; == GUI generated with Koda ==

$xcfhxfhxfhger = GUICreate("Chat Log", 361, 37, 191, 125, BitOR($WS_CAPTION,$WS_BORDER,$WS_CLIPSIBLINGS))

$prgProgress = GUICtrlCreateProgress(8, 8, 345, 17)

GUISetState(@SW_SHOW)

$MonsterMissed = 0

$MonsterAttacked = 0

$MonsterDied = 0

; Check if file opened for reading OK

If $file = -1 Then

MsgBox(0, "Error", "Unable to open file.")

Exit

EndIf

$TotalLines = _FileCountLines(@ScriptDir & "\chat.log")

$CurrentLine = 1

; Read in lines of text until the EOF is reached

While 1

;Update progress bar

GUICtrlSetData($prgProgress, 100 * ($CurrentLine / $TotalLines))

$line = FileReadLine($file)

If @error = -1 Then ExitLoop

If StringRegExp($line, '( .*?) attacks you and misses!', 0) Then

$as_monstermiss = StringRegExp($line, '( .*?) attacks you and misses!', 3)

;MsgBox(0,"","Attacked by: " & $as_monstermiss[0])

$MonsterMissed = $MonsterMissed + 1

EndIf

If StringRegExp($line, "hits your (.*?) for",0) Then

$as_monsterattacksyou = StringRegExp($line, "hits your (.*?) for",3)

;MsgBox(0,"","He hit my: " & $as_monsterattacksyou[0])

$MonsterAttacked = $MonsterAttacked + 1

EndIf

If StringRegExp($line, "( .*?) dies!",0) Then

$as_monsterdead = StringRegExp($line, "( .*?) dies!",3)

;MsgBox(0,"","I killed: " & $as_monsterdead[0])

$MonsterDied = $MonsterDied + 1

EndIf

$CurrentLine = $CurrentLine + 1

WEnd

MsgBox(0, "", "Stats: " & $MonsterMissed & ":" & $MonsterAttacked & ":" & $MonsterDied)

FileClose($file)

“Give a man a script; you have helped him for today. Teach a man to script; and you will not have to hear him whine for help.”AutoIt4UE - Custom AutoIt toolbar and wordfile for UltraEdit/UEStudio users.AutoIt Graphical Debugger - A graphical debugger for AutoIt.SimMetrics COM Wrapper - Calculate string similarity.

Link to comment
Share on other sites

Thanks once again (im gonna try your script!)

I was toying around with ProgressOn/Set/Off

Ive tried this before but having some "math" problems.

First i checked how many lines the .log had, i saved the number as a variable.

After that i added another variable in the loop, increasing by 1 every line that passed. It seems to easy in theory so when $x = $totallines progress = 100%

But it turned out fare more complicated. What made it so confussing was this:

ProgressOn("Progress Meter", "Increments every second", "0 percent")
For $i = 10 to 100 step 10
    sleep(1000)
    ProgressSet( $i, $i & " percent")
Next
ProgressSet(100 , "Done", "Complete")
sleep(500)
ProgressOff()

The For $i = 10 to 100 step 10 <- The step part, made all the mess.

Lets say my logs contains 14000 lines (which it does), $x being increased everytime in the loop as it checks the lines. Arrrgh.. brain failure.

Whats the "math" to get the "step" part to be 1% of the total amount of lines... im explaining horribly atm.

<Edit> Just ran your progressbar, thanks a ton.. will use this in other scripts! :o </Edit>

Edited by huldu

"I'm paper, rock is fine, nerf scissors!!!"

Link to comment
Share on other sites

  • Moderators

Where $NArray[0] carries the total amount of lines to parse in the file:

For $i = 1 To $NArray[0]
    ProgressSet($i / $NArray[0] * 100, Int($i / $NArray[0] * 100) & "%")
Next

Edit:

Code Tags :o

Edit2:

A working example:

Local $NArray[1]
$NArray[0] = 14400
ProgressOn('Title', 'MainText', 'SubText')
Local $Timer = TimerInit()
For $i = 1 To $NArray[0]
;ProgressSet($i / $NArray[0] * 100, Int($i / $NArray[0] * 100) & "%")
    ProgressSet($i / $NArray[0] * 100, '', 'We are ' & Int($i / $NArray[0] * 100) & '% completed')
    If TimerDiff($Timer) >= 1 Then
        Sleep(10)
        $Timer = TimerInit()
    EndIf
Next
ProgressOff()
I put the Timer in there just to give the affect as if it were actually parsing a file, otherwise it would go really really fast! Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

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...