Jump to content

Recommended Posts

Posted (edited)

My script currently reads a text file line by line. I want it to look for this the chat message below and skip the session messages. 

  Quote

Session Close: Sun Jan 22 02:00:35 2017

Session Start: Sun Jan 22 10:14:57 2017

[10:15] <Cormin> heres my chat message

Expand  

There are some garbage lines that I want it to skip over. So if a line doesnt match that format then skip it. I tried experimenting with StringRegExp but I think I'm using it wrong. Heres my code

If StringRegExp($read, '\W\d\d\w\d\d\D.\W') = Not @error Then

I was hoping that this would be able to see the open bracket, 2 digits, a colon, 2 digits,  close bracket, a space and a <. But it doesn't seem to be working.

 

Eventually I want to be able to extract the username and chat message into separate strings but I haven't got that far yet.

Edited by Cormin
  • Moderators
Posted

Cormin,

This works for me:

#include <Array.au3>
#include <StringConstants.au3>

$aLines = FileReadToArray("IRC.txt")

_ArrayDisplay($aLines, "", Default, 8)

Global $aExtract[UBound($aLines) - 1][2]    ; Array to hold the extracted data
Global $iIndex = 0                          ; Index of array to fill
Global $sDelim = "~~~~~"                    ; Choose a string that is very unlikely to appear in the message

For $i = 0 To UBound($aLines) - 1
    ; Loop through the lines and check if they match a message line - extract the name and the message
    $sExtract = StringRegExpReplace($aLines[$i], "^.*<(\w+)>\s(.*)$", "$1" & $sDelim & "$2")
    ; Split the line
    $aSplit = StringSplit($sExtract, $sDelim, $STR_ENTIRESPLIT)
    ; Save the the array
    If Not @error Then
        $aExtract[$iIndex][0] = $aSplit[1]
        $aExtract[$iIndex][1] = $aSplit[2]
        ; Increase the index
        $iIndex += 1
    EndIf

Next

; Correctly size the extracted array and show it
ReDim $aExtract[$iIndex][2]
_ArrayDisplay($aExtract, "", Default, 8)

with this file:

Session Close: Sun Jan 22 02:00:35 2017 

Session Start: Sun Jan 22 10:14:57 2017 

garbage

[10:15] <Cormin> heres my chat message1

[10:15] <Cormin> heres my chat message2

garbage

[10:15] <Cormin> heres my chat message3

[10:15] <Cormin> heres my chat message4

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

  Reveal hidden contents

 

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
×
×
  • Create New...