Jump to content
Sign in to follow this  
kaotkbliss

StringRegExp not returning an array

Recommended Posts

kaotkbliss

I'm trying to create somewhat of a log combining information from 2 different sources. I've gotten most of it to work. Here is the code

#Include <File.au3>
#Include <Array.au3>
Opt("SendKeyDelay", 45) ; typing delay 45 milliseconds
Run("Notepad.exe")
Sleep(500)
WinWaitActive("Untitled")
Sleep(500)
$num=_FileListToArray("L:\Client\LLEU\Daily\Print\PDF Copies","*",1)
For $i=1 To $num[0]
    ControlSend("Untitled","","Edit1",$num[$i]&" ")
    Sleep(500)
    $file=StringTrimRight($num[$i],12)
    Sleep(500)
    Run("Notepad.exe C:\Program Files\Simpo PDF to Text\Output\"&$file&"Record Count.txt")
    WinWaitActive($file&"Record Count.txt")
    Sleep(500)
    $ireland=StringTrimRight($num[$i],4)
    Sleep(500)
    $ireland2=StringTrimLeft($ireland,16)
    Sleep(500)
    $result=StringRegExp("C:\Program Files\Simpo PDF to Text\Output\"&$file&"Record Count.txt",$ireland2&": ([1-9]{1,3})",1)
    Sleep(500)
    ControlSend("Untitled","","Edit1",$result[0]&" records"&@CR)
    WinClose($file&"Record Count.txt")
    Sleep(1500)
    Next

The file name is: LLEU1119_ABN1_1_4_PG_NI.pdf

and after trimming for the SRE the variable $ireland2 is 4_Page_NI

the text it is searching through is this:

Name Pages

Persons Name 09-1027_NMB,A 0-0-0-0-0-1-8-0-0-0-0-0

Records Total Pages (1 sided):

3_Page_NI: 0 0

4_Page_NI: 1 8

5_Page_NI: 0 0

3_Page_IE: 4 24

4_Page_IE: 28 224

5_Page_IE: 0 0

Total Records: 33

--------------------------------------------------

the output enters the pdf name in an untitled notepad:

LLEU1119_ABN1_1_4_PG_NI.pdf

and what I am trying to do is find the corresponding record count for the pdf

and place it behind the pdf name so it shows like this:

LLEU1119_ABN1_1_4_PG_NI.pdf 1 records

Everything seems fine except the SRE

if I remove the [0] after the $result variable then it will always enter a 0 if I leave it I recieve an error stating that $result is not an array.


010101000110100001101001011100110010000001101001011100110010000

001101101011110010010000001110011011010010110011100100001

My Android cat and mouse game
https://play.google.com/store/apps/details?id=com.KaosVisions.WhiskersNSqueek

We're gonna need another Timmy!

Share this post


Link to post
Share on other sites
kaotkbliss

Ok, I'm not trying to bump my own post but I think I know why it's not returning an array, I believe the SRE is searching the path string and not in the file itself. So how would I have the SRE search inside the text file?

I think I remember some posts where people were doing text replaces and searches so I will look for those and see what I can dig up.


010101000110100001101001011100110010000001101001011100110010000

001101101011110010010000001110011011010010110011100100001

My Android cat and mouse game
https://play.google.com/store/apps/details?id=com.KaosVisions.WhiskersNSqueek

We're gonna need another Timmy!

Share this post


Link to post
Share on other sites
jvanegmond

I did see you were searching the path string. But to be honest, before I was going to mention it I wanted a good post ready explaining all the things wrong with your code. And there are a lot. I'll leave most of them for now, I figure you can find at least half of them by having another close look at your own code instead of copy pasting everything.

As far as your immediate problem goes: Read the contents of the file with the FileRead function.

Share this post


Link to post
Share on other sites
kaotkbliss

I just found that and added it to the code and removed

Run("Notepad.exe C:\Program Files\Simpo PDF to Text\Output\"&$file&"Record Count.txt")
    WinWaitActive($file&"Record Count.txt")
    Sleep(500)

as it's not needed with the readfile.

however, it is still not returning the value I need. I've tried variations of the pattern such as "(?:"&$ireland2&": )([1-9]{1,3})"

But that did not work either.

according to the help file whatever is in the () in the pattern is what is to be captured in the array (except if there is (?:

so I even tried just $ireland2&": ([1-9]{1,3})"


010101000110100001101001011100110010000001101001011100110010000

001101101011110010010000001110011011010010110011100100001

My Android cat and mouse game
https://play.google.com/store/apps/details?id=com.KaosVisions.WhiskersNSqueek

We're gonna need another Timmy!

Share this post


Link to post
Share on other sites
jvanegmond

Check the value of @error and look up what this means in the help file. Your pattern may not be malformed, but it may not be returning any results because it's not what you expected.

Share this post


Link to post
Share on other sites
GEOSoft

One problem you will be having with those last two SREs is the "$". Thats a reserved character in SRE and means ens of the string (same as \z) so if you are using it as a literal then you have to escape it thusly "\$".


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites
kaotkbliss

@error is returning a 1 match not found and I can't figure out why. $ireland2 is returning the correct variable that should match

I tried adding \ in front of the $ as GEOSoft suggested, but I get an illegal character error

if I enclose it in quotes the I have to enter it as such: "\"&$ireland2

which completely changes the meaning and results in bad pattern error.


010101000110100001101001011100110010000001101001011100110010000

001101101011110010010000001110011011010010110011100100001

My Android cat and mouse game
https://play.google.com/store/apps/details?id=com.KaosVisions.WhiskersNSqueek

We're gonna need another Timmy!

Share this post


Link to post
Share on other sites
kaotkbliss

after some messing around and trying variations of the pattern I decided to go one step at a timeand try capturing and returning the variable instead of what I eventually want to return.

It appears that the problem lies somewhere in the pattern format for the variable as the variable itself is returning the value it needs to.


010101000110100001101001011100110010000001101001011100110010000

001101101011110010010000001110011011010010110011100100001

My Android cat and mouse game
https://play.google.com/store/apps/details?id=com.KaosVisions.WhiskersNSqueek

We're gonna need another Timmy!

Share this post


Link to post
Share on other sites
GEOSoft

I led you astray. You were not using the $ as a literal.

I don't have time to re-write what you have but it looks like the problem is here

$result=StringRegExp("C:\Program Files\Simpo PDF to Text\Output\"&$file&"Record Count.txt",$ireland2&": ([1-9]{1,3})",1)

I think what you want is FileRead()

$sStr = FileRead("C:\Program Files\Simpo PDF to Text\Output\"&$file&"Record Count.txt")
$Result = StringRegExp($sStr, SRE goes here, 1)

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites
kaotkbliss

yeah, I fixed that part. messing around a bit more with the pattern, @error is no longer flagged but $result is returning a 1 (match found) and not the actual result. And now I'm back to my original problem that if I add [0] to the end of $result in the

ControlSend("Untitled","","Edit1",$result[0]&" records"&@CR)

I get the error

>"C:\Program Files\AutoIt3\SciTE\..\autoit3.exe" /ErrorStdOut "C:\test.au3"

C:\test.au3 (21) : ==> Subscript used with non-Array variable.:

ControlSend("Untitled","","Edit1",$result[0]&" records"&@CR)

ControlSend("Untitled","","Edit1",$result^ ERROR

>Exit code: 1 Time: 6.462

if I remove the [0] $result returns 1 every time (not just for the pdfs that have 1 record)

-edit-

current code is:

#Include <File.au3>
#Include <Array.au3>
Opt("SendKeyDelay", 45) ; typing delay 45 milliseconds
Run("Notepad.exe")
Sleep(500);|
WinWaitActive("Untitled")
Sleep(500)
$num=_FileListToArray("L:\Client\LLEU\Daily\Print\PDF Copies","*",1)
For $i=1 To $num[0]
    ControlSend("Untitled","","Edit1",$num[$i]&" ")
    Sleep(500)
    $file=StringTrimRight($num[$i],12)
    Sleep(500)
    $ireland=StringTrimRight($num[$i],4)
    Sleep(500)
    $ireland2=StringTrimLeft($ireland,16)
    Sleep(500)
    $text=FileRead("C:\Program Files\Simpo PDF to Text\Output\"&$file&"Record Count.txt")
    $result=StringRegExp($text,'(?:'&$ireland2&': )([1-9]{1,3})',1)
    Sleep(500)
    ControlSend("Untitled","","Edit1",$result[0]&" records"&@CR)
    WinClose($file&"Record Count.txt")
    Sleep(1500)
    Next

-edit-

@error came back, unless I change the SRE flag to anything but 1. It still does not return the correct result though.

Edited by kaotkbliss

010101000110100001101001011100110010000001101001011100110010000

001101101011110010010000001110011011010010110011100100001

My Android cat and mouse game
https://play.google.com/store/apps/details?id=com.KaosVisions.WhiskersNSqueek

We're gonna need another Timmy!

Share this post


Link to post
Share on other sites
GEOSoft

Like Manadar pointed out, all of your code needs work and on top of that I don't see (in your sample) where there is anything about records except Total Records: 33 so i don't know what to try for a match if I don't know what line you are attempting to match.


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites
PsaltyDS

The value of @error was reset by Sleep(). You are getting an @error from StringRegExp() and the return value should be ignored. Add some error handling:

$result = StringRegExp($text, '(?:' & $ireland2 & ': )([1-9]{1,3})', 1)
    If @error Then 
        MsgBox(16, "RegExp Error", "@error = " & @error & "; @extended = " & @extended)
    Else
        Sleep(500)
        ControlSend("Untitled", "", "Edit1", $result[0] & " records" & @CR)
    EndIf
    WinClose($file & "Record Count.txt")


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites
kaotkbliss

Like Manadar pointed out, all of your code needs work and on top of that I don't see (in your sample) where there is anything about records except Total Records: 33 so i don't know what to try for a match if I don't know what line you are attempting to match.

What $ireland2 returns is: 4_Page_NI

the number of records is what follows that in the text ": 1"

The pdfs are named with 4_Page_NI, 3_Page_IE, or whatever in their name.

The text has part of the pdf name followed by the number of records in the pdf and then the number of pages in the pdf

the total records at the very end is just a total count from all pdfs


010101000110100001101001011100110010000001101001011100110010000

001101101011110010010000001110011011010010110011100100001

My Android cat and mouse game
https://play.google.com/store/apps/details?id=com.KaosVisions.WhiskersNSqueek

We're gonna need another Timmy!

Share this post


Link to post
Share on other sites
GEOSoft

$sText = FileRead("SomeFile.txt")
$sSRE = "(?i)(?m:^)" & $Ireland2 & ":\s*(\d+)"
$aSre = StringRegExp($sText, $sSRE, 1)
If Not @Error Then
    MsgBox(0, "Result", $aSRE[0])
EndIf


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites
kaotkbliss

I need to figure out why @error = 1 in the first place because the string returned by $ireland2 is clearly in the text.

After trimming of the file name LLEU1119_ABN1_1_4_PG_NI.pdf

$ireland2= 4_Page_NI

the text being read is:

Name Pages

Persons Name 09-1027_NMB,A 0-0-0-0-0-1-8-0-0-0-0-0

Records Total Pages (1 sided):

3_Page_NI: 0 0

4_Page_NI: 1 8 <--- this line it should find a match

5_Page_NI: 0 0

3_Page_IE: 4 24

4_Page_IE: 28 224

5_Page_IE: 0 0

Total Records: 33

--------------------------------------------------


010101000110100001101001011100110010000001101001011100110010000

001101101011110010010000001110011011010010110011100100001

My Android cat and mouse game
https://play.google.com/store/apps/details?id=com.KaosVisions.WhiskersNSqueek

We're gonna need another Timmy!

Share this post


Link to post
Share on other sites
PsaltyDS

I need to figure out why @error = 1 in the first place because the string returned by $ireland2 is clearly in the text.

After trimming of the file name LLEU1119_ABN1_1_4_PG_NI.pdf

$ireland2= 4_Page_NI

the text being read is:

Name Pages

Persons Name 09-1027_NMB,A 0-0-0-0-0-1-8-0-0-0-0-0

Records Total Pages (1 sided):

3_Page_NI: 0 0

4_Page_NI: 1 8 <--- this line it should find a match

5_Page_NI: 0 0

3_Page_IE: 4 24

4_Page_IE: 28 224

5_Page_IE: 0 0

Total Records: 33

--------------------------------------------------

The RegExp works fine here:
Global $result, $ireland2 = "4_Page_NI"
Global $aStrings[6] = ["3_Page_NI: 0 0", _
        "4_Page_NI: 1 8", _
        "5_Page_NI: 0 0", _
        "3_Page_IE: 4 24", _
        "4_Page_IE: 28 224", _
        "5_Page_IE: 0 0"]

For $i = 0 To UBound($aStrings) - 1
    $result = StringRegExp($aStrings[$i], '(?:' & $ireland2 & ': )([1-9]{1,3})', 1)
    If @error Then
        ConsoleWrite($i & ":  @error = " & @error & "; @extended = " & @extended & @LF)
    Else
        ConsoleWrite($i & ":  " & $result[0] & " records" & @LF)
    EndIf
Next

Something is not as you described it.

:)

Edit: Since you seem to mean the whole thing is passed as one string, I tried it this way too (still worked):

Global $result, $ireland2 = "4_Page_NI"
Global $sString = "Name Pages" & @CRLF & _
        "" & @CRLF & _
        "Persons Name 09-1027_NMB,A 0-0-0-0-0-1-8-0-0-0-0-0" & @CRLF & _
        "" & @CRLF & _
        "Records Total Pages (1 sided):" & @CRLF & _
        "3_Page_NI: 0 0" & @CRLF & _
        "4_Page_NI: 1 8" & @CRLF & _
        "5_Page_NI: 0 0" & @CRLF & _
        "3_Page_IE: 4 24" & @CRLF & _
        "4_Page_IE: 28 224" & @CRLF & _
        "5_Page_IE: 0 0" & @CRLF & _
        "Total Records: 33" & @CRLF & _
        "--------------------------------------------------"

$result = StringRegExp($sString, '(?:' & $ireland2 & ': )([1-9]{1,3})', 1)
If @error Then
    ConsoleWrite("@error = " & @error & "; @extended = " & @extended & @LF)
Else
    ConsoleWrite($result[0] & " records" & @LF)
EndIf

;)

Edited by PsaltyDS

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites
kaotkbliss

$sText = FileRead("SomeFile.txt")
$sSRE = "(?i)(?m:^)" & $Ireland2 & ":\s*(\d+)"
$aSre = StringRegExp($sText, $sSRE, 1)
If Not @Error Then
    MsgBox(0, "Result", $aSRE[0])
EndIf

still recieving @error when there should be none. It has to be something wrong with my pattern.

the pdfs are always named

job|month|day|#_Page_|NI or IE|.pdf

the text is always

#_Page_|NI or IE|: # #


010101000110100001101001011100110010000001101001011100110010000

001101101011110010010000001110011011010010110011100100001

My Android cat and mouse game
https://play.google.com/store/apps/details?id=com.KaosVisions.WhiskersNSqueek

We're gonna need another Timmy!

Share this post


Link to post
Share on other sites
GEOSoft

One last attempt. Extract the files from the zip to your desktop and run the au3 file. What do you get? It should be 1 the first time and 28 the second, or at least that's what I get and according to what you posted that is correct.


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites
kaotkbliss

The RegExp works fine here:

Global $result, $ireland2 = "4_Page_NI"
Global $aStrings[6] = ["3_Page_NI: 0 0", _
        "4_Page_NI: 1 8", _
        "5_Page_NI: 0 0", _
        "3_Page_IE: 4 24", _
        "4_Page_IE: 28 224", _
        "5_Page_IE: 0 0"]

For $i = 0 To UBound($aStrings) - 1
    $result = StringRegExp($aStrings[$i], '(?:' & $ireland2 & ': )([1-9]{1,3})', 1)
    If @error Then
        ConsoleWrite($i & ":  @error = " & @error & "; @extended = " & @extended & @LF)
    Else
        ConsoleWrite($i & ":  " & $result[0] & " records" & @LF)
    EndIf
Next

Something is not as you described it.

:)

I tried to give as much detail as I could but because I cannot share the pdfs and texts that the code is looking at, it makes it difficult to completely convey everything.

I can say that I even went as far as to insert MsgBox right after every variable call to display the current variable value for all variables in the script and at each step everything returned expected values.

For some reason SRE is not finding the text in my script.

here is a stepbystep

#Include <File.au3>
#Include <Array.au3>
Opt("SendKeyDelay", 45)
Run("Notepad.exe")
Sleep(500)
WinWaitActive("Untitled")
Sleep(500)
$num=_FileListToArray("L:\Client\LLEU\Daily\Print\PDF Copies","*",1)
MsgBox(0,"$num",$num)
For $i=1 To $num[0]
    ControlSend("Untitled","","Edit1",$num[$i]&" ")
    MsgBox(0,"$num[$i]",$num[$i])
    Sleep(500)
    $file=StringTrimRight($num[$i],12)
    MsgBox(0,"$file",$file)
    MsgBox(0,"$num[$i]",$num[$i])
    Sleep(500)
    $ireland=StringTrimRight($num[$i],4)
    MsgBox(0,"$ireland",$ireland)
    MsgBox(0,"$num[$i]",$num[$i])
    Sleep(500)
    $ireland2=StringTrimLeft($ireland,16)
    MsgBox(0,"$ireland2",$ireland2)
    MsgBox(0,"$ireland",$ireland)
    Sleep(500)
    $text=FileRead("C:\Program Files\Simpo PDF to Text\Output\"&$file&"Record Count.txt")
    MsgBox(0,"$text",$text)
    $result=StringRegExp($text,'(?:'&$ireland2&': )([1-9]{1,3})',1)
    MsgBox(0,"$result",$result)
    MsgBox(0,"$text",$text)
    MsgBox(0,"$ireland2",$ireland2)
    ControlSend("Untitled","","Edit1",$result[0]&" records"&@CR)
    Sleep(1500)
    Next

Posted Image

Posted Image

Posted Image

Posted Image


010101000110100001101001011100110010000001101001011100110010000

001101101011110010010000001110011011010010110011100100001

My Android cat and mouse game
https://play.google.com/store/apps/details?id=com.KaosVisions.WhiskersNSqueek

We're gonna need another Timmy!

Share this post


Link to post
Share on other sites
kaotkbliss

One last attempt. Extract the files from the zip to your desktop and run the au3 file. What do you get? It should be 1 the first time and 28 the second, or at least that's what I get and according to what you posted that is correct.

yes, I did get 1 and 28 as well.........

I'm lost


010101000110100001101001011100110010000001101001011100110010000

001101101011110010010000001110011011010010110011100100001

My Android cat and mouse game
https://play.google.com/store/apps/details?id=com.KaosVisions.WhiskersNSqueek

We're gonna need another Timmy!

Share this post


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
Sign in to follow this  

×