Zeichen in Datei per regulärem Ausdruck tauschen

  • Hallo miteinander,

    ich möchte gerne aus eine Text-Datei Zeichen suchen und tauschen, Allerdings bin ich noch neu in der AutoIt Welt :)

    Mein String nach dem ich suche ist meist so aufgebaut: PC1,254,2,244
    Also immer die Zeichen PC, gefolgt von 1-3 zahlen, gefolgt von einem , 1-3 Zahlen usw.

    Mein regulärer Ausdruck wäre dafür: PC\d{1,3},\d{1,3},\d{1,3},\d{1,3}

    Jetzt hab ich mal ein Script angefangen, aber irgendwie macht das garnichts,
    wer kann mit nen Schubs in die richtige Richtung geben?

    [autoit]


    local $file = FileOpen ("test.gl2")

    [/autoit][autoit][/autoit][autoit]

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

    [/autoit][autoit][/autoit][autoit]

    while 1

    local $chars = Fileread($file)
    if @error = -1 Then ExitLoop
    WEnd
    local $regex = "PC\d{1,3},\d{1,3},\d{1,3},\d{1,3}"
    local $replace = "PC1,0,0,0"
    local $text = StringReplace ($chars, $regex, $replace)
    filewrite ( "output.gl2", $text)

    [/autoit][autoit][/autoit][autoit]

    FileClose($file)

    [/autoit]

    Einmal editiert, zuletzt von melman01 (1. Februar 2013 um 12:41)

  • Spoiler anzeigen
    [autoit]


    local $fileRead = FileOpen ("test.gl2")

    [/autoit] [autoit][/autoit] [autoit]

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

    [/autoit] [autoit][/autoit] [autoit]

    while 1

    local $chars = Fileread($fileRead)
    if Not @error Then ExitLoop
    WEnd
    FileClose($fileRead)
    local $regex = "PC\d{1,3},\d{1,3},\d{1,3},\d{1,3}"
    local $replace = "PC1,0,0,0"
    local $text = StringRegExpReplace ($chars, $regex, $replace)

    [/autoit] [autoit][/autoit] [autoit]

    $fileWrite = FileOpen("output.gl2",2)
    FileWrite ( $fileWrite, $text)
    FileClose($fileWrite)

    [/autoit]

    Zudem gibt (zumindest bei mir) @errror, bei der Funktion Fileread() eine 0 (NULL) zurück wenn es erfolgreich war, außer du benützt dem Parameter Count.

  • Also ich hab das mal ersetzt, aber ich glaube dass ich mit File Open oder File Read einen Fehler mache, im
    Moment macht das Script garnichts..


    [autoit]


    local $file = FileOpen ("test.gl2")

    [/autoit][autoit][/autoit][autoit]

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

    [/autoit][autoit][/autoit][autoit]

    while 1

    local $chars = Fileread($file)
    if @error = -1 Then ExitLoop
    WEnd
    local $text = StringRegExpReplace($chars,'PC\d{1,3},\d{1,3},\d{1,3},\d{1,3}','PC1,0,0,0')
    filewrite ("output.gl2", $text)

    [/autoit][autoit][/autoit][autoit]

    FileClose($file)
    Exit

    [/autoit]