Jump to content

basic regex search and replace


Recommended Posts

I am completely new to AutoIT. I'm wondering how easy it might be for me to do something. Basically, I want to do some regex search/replace's on a file and then remove the empty lines, and save it to a new name.

Basically the file, has lines like this (which I want to delete entirely)

----------------------- Start Radar session 12:23:41 20/06/07 -----------------------
----------------------- End Radar session 12:23:45 20/06/07 -----------------------
12:19:28 20/06/07 Fighting Bloodfury Harpy (Target-Health 100%)
12:23:43 20/06/07 Stopped
12:22:37 20/06/07 Alive (Health 100%,Mana 79%)

And it has lines like this

12:19:02 20/06/07 Alive  (Health 100%,Mana 93%): 1547.369995 775.409973

that I want to change to be like this

<Waypoint>775.40 1547.36</Waypoint>

The numbers can be negative, and there is always 6 decimal points in the original file, and always need to be 2 in the output file. (also the number swap positions)

Is this something that would be pretty easy with autoit?

Link to comment
Share on other sites

Yep, quite easily :rolleyes:

Opt("TrayIcondebug",1)

$rein = FileOpenDialog("Sourcefile","","Textdateien (*.txt)")
$raus = FileSaveDialog("Targetfile","","Textdateien (*.txt)")

$zeile_alt = ""
$zeile = ""

$lesen = FileOpen($rein,0)
If $lesen = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

$schreiben = FileOpen($raus,2)
If $schreiben = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

While 1
    $zeile = FileReadLine($lesen)
    If @error <> 0 Then ExitLoop
    if StringRegExp ( $zeile, "Alive .*\..*$",0,0 ) = 1 Then
    $a = StringRegExpReplace($zeile,".+?(\d+?\.\d{1,2}).*? (\d+?\.\d{2}).*","\2 \1")
          FileWriteLine($schreiben, "<Waypoint>" & $a & "</Waypoint>")
    EndIf           
    
    $zeile_alt = $zeile 
Wend

FileClose($schreiben)
FileClose($lesen)

Have fun with your WoW Farmbot (I guess ^^)

Any of my own codes posted on the forum are free for use by others without any restriction of any kind. (WTFPL)

Link to comment
Share on other sites

You rock! Thank you so much! :rolleyes:

This solves my problem and it'll help me learn autoit because I learn best from examples that deal with my own real life problems.

The only problem with it, is it didn't handle the negative numbers correctly. (which I only mentioned, and didn't include any examples) I also changed the selection regex, because I realized not all the lines will say alive. And lastly, (though not needed since there are always 6 decimal places in the 'in' file) in the StringRegExpReplace I changed {1,2} to {2}

Opt("TrayIcondebug",1)

$rein = FileOpenDialog("Sourcefile","","Textdateien (*.log)")
$raus = FileSaveDialog("Targetfile","","Textdateien (*.xml)")

$zeile_alt = ""
$zeile = ""

$lesen = FileOpen($rein,0)
If $lesen = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

$schreiben = FileOpen($raus,2)
If $schreiben = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

While 1
    $zeile = FileReadLine($lesen)
    If @error <> 0 Then ExitLoop
    if StringRegExp ( $zeile, ".*\..*\..*$",0,0 ) = 1 Then
    $a = StringRegExpReplace($zeile,".+?(-?\d+?\.\d{1,2}).*? (-?\d+?\.\d{2}).*","\2 \1")
          FileWriteLine($schreiben, "<Waypoint>" & $a & "</Waypoint>")
    EndIf        
   
    $zeile_alt = $zeile 
Wend

FileClose($schreiben)
FileClose($lesen)

All in all, very minor changes considering you had nothing to test it on.

So yea -- works like a charm and thanks again.

Two questions, just to help me make sure I understand everything,

I understand pretty much everything its doing, but I am confused about what the "$zeile_alt = $zeile" is for.

And also on the StringRegExpReplace -- I was playing around with the file that it is converting, just to see if {1,2} and {2} meant what I thought they did (which they do) -- but I was confused by the fact that for this line (This would never really be in the file, I just made it for testing purposes to see if I understood what it's doing by being able to predict the output)

12:19:02 20/06/07 Alive  (Health 100%,Mana 93%): 1547. 1775.

It returned the whole line the the 'out' file. I couln't understand why it would do this. It seems like the StringRegExpReplace would give nothing, because I wouldn't think that anything could match either of the groups. So I would have predicted that it would give a blank line, or nothing at all.

My only guess as to why, is that if nothing matched in the StringRegExpReplace, that it returns the whole line. Is this the case?

Link to comment
Share on other sites

Oh and one more question. I added a MsgBox tot he end to inform of successful script completion, and I was wondering, is there a variable I can use to display the name of the output file?

--EDIT--

Nevermind -- Shoudla given it a try before asking. I figured it out. :rolleyes:

Edited by bdb4269
Link to comment
Share on other sites

I understand pretty much everything its doing, but I am confused about what the "$zeile_alt = $zeile" is for.

Ooops. :rolleyes: Well, I've already had a little script to convert a text, and it had a simple filter to remove duplicate lines: it memorized the last line read as $zeile_alt and if the next line in the file had the same content, the line was skipped using a "if $zeile=$zeile_alt then ContinueLoop". Erased the IF statement, but forgot to remove this line, sorry about that.

My only guess as to why, is that if nothing matched in the StringRegExpReplace, that it returns the whole line. Is this the case?

Correct. There is a similar command (StringReplace), which makes easier to understand what happens: StringReplace($x,".","-") would change every "." it finds into a "-". If the $x does not contain a dot, it stays unchanged. So, if the Regex does not find anything... it does nothing.

While fiddling around with the syntax this morning, I stumbled about the very same thing :rambo:

By the way, for testing RegEx expressions: http://weitz.de/regex-coach/. Nice freeware toy.

Have fun,

Marc

Any of my own codes posted on the forum are free for use by others without any restriction of any kind. (WTFPL)

Link to comment
Share on other sites

Correct. There is a similar command (StringReplace), which makes easier to understand what happens: StringReplace($x,".","-") would change every "." it finds into a "-". If the $x does not contain a dot, it stays unchanged. So, if the Regex does not find anything... it does nothing.

While fiddling around with the syntax this morning, I stumbled about the very same thing :rolleyes:

By the way, for testing RegEx expressions: http://weitz.de/regex-coach/. Nice freeware toy.

Have fun,

Marc

Yea -- that does make more sense, when I think about it in the context of StringReplace.

And I checked out that regex coach. It looks awesome. Just the kind a thing I need to hone my regex skills. :x

Thanks again for all the help!

(as a kinda funny side note -- the author of the software that created the log I was parsing, ended up putting out a version yesterday, that outputs the log as an .xml file in the needed format. But, either way, I'm still totally glad I came here. AutoIt, has just become next languange I'm gonna learn. :rambo: )

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