Jump to content

Recommended Posts

i have a script that is supposed to extract text that includes or starts with the "@" character but it doesn't seem to work

 

what is the problem with the regex?

 

#include <File.au3>
#include <Array.au3>
#include <MsgBoxConstants.au3>

_Example()
Func _Example()
    ; Error monitoring. This will trap all COM errors while alive.
    ; This particular object is declared as local, meaning after the function returns it will not exist.
    Local $oErrorHandler = ObjEvent("AutoIt.Error", "_ErrFunc")

    Local $oDictionary = ObjCreate("Scripting.Dictionary")
    Local $mypath = @ScriptDir
    Local $aFiles = _FileListToArrayRec($mypath, "*.txt", 1, 1)
    Local $aWords

    If @error Then
        MsgBox($MB_SYSTEMMODAL, "Error", "No files found")
        Exit
    Else
        MsgBox($MB_SYSTEMMODAL, "Found", $aFiles[0] & " files")
    EndIf

    Local $aWords
    For $i = 1 To $aFiles[0]
        $aWords = StringRegExp(FileRead($aFiles[$i]), "(?mi)^\s*(@.*)$", 3) ; change pattern to fit your definition of "word
        Local $iError = @error
        If $iError = 0 Then
            For $Word In $aWords
                $oDictionary.add($Word, $Word)
            Next
        Else
            ;;MsgBox($MB_SYSTEMMODAL, "Error", $aFiles[$i] & " - " & $i & @CRLF & "error: " & $iError)
        EndIf
    Next

    $aWords = $oDictionary.Items
    FileWrite("New AutoIt v3 Script - acomercial.txt", _ArrayToString($aWords, @CRLF))

EndFunc   ;==>_Example


; User's COM error function. Will be called if COM error occurs
Func _ErrFunc($oError)
    ; Do anything here.
    ConsoleWrite(@ScriptName & " (" & $oError.scriptline & ") : ==> COM Error intercepted !" & @CRLF & _
            @TAB & "err.number is: " & @TAB & @TAB & "0x" & Hex($oError.number) & @CRLF & _
            @TAB & "err.windescription:" & @TAB & $oError.windescription & @CRLF & _
            @TAB & "err.description is: " & @TAB & $oError.description & @CRLF & _
            @TAB & "err.source is: " & @TAB & @TAB & $oError.source & @CRLF & _
            @TAB & "err.helpfile is: " & @TAB & $oError.helpfile & @CRLF & _
            @TAB & "err.helpcontext is: " & @TAB & $oError.helpcontext & @CRLF & _
            @TAB & "err.lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _
            @TAB & "err.scriptline is: " & @TAB & $oError.scriptline & @CRLF & _
            @TAB & "err.retcode is: " & @TAB & "0x" & Hex($oError.retcode) & @CRLF & @CRLF)
EndFunc   ;==>_ErrFunc

 

Link to post
Share on other sites

Please be careful when defining the requirements
This :

23 hours ago, vinnyMS said:

extract text that includes or starts with the "@" character

means that "@buy", "@sell", "@main" and "@exit" should match  ;)

Edit
BTW your regx didn't work because you omitted the brackets (as Nine pointed out)
If you mention them, then miraculously it works "(?mi)^\s*\[(@.*)\]$"  :)

Edited by mikell
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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...