Jump to content
Sign in to follow this  
MyEarth

Help with RegExp

Recommended Posts

MyEarth

Hello guys

I have a commandline and i need to extract the information in real-temp

#include <Constants.au3>

Local $foo = Run('mycommand.exe', @SystemDir, @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD)
Local $data

While True
    $data &= StdoutRead($foo)
    If @error Then ExitLoop
    ConsoleWrite($data & @CRLF)
    Sleep(25)
WEnd

This is a sample output:

"(TEXT)","\\TEXT\TEXT\TEXT"
"07/03/2014 10:47:55.183","0.000000"
"07/03/2014 10:48:06.216","4.122662"
"(TEXT)","\\TEXT\TEXT\TEXT"
"07/03/2014 10:48:18.788","1.665872"
"07/03/2014 10:48:29.558","2.677412"
"(TEXT)","\\TEXT\TEXT\TEXT"
"07/03/2014 10:48:39.112","0.199651"
"(TEXT)","\\TEXT\TEXT\TEXT"

If need ony the part with date-time-numbers:

"07/03/2014 10:47:55.183","0.000000"

And i need to exclude, in addition to other "text", the duplicate lines like:

"07/03/2014 10:47:55.183","0.000000"
"07/03/2014 10:47:55.183","0.000000" ; <<<<< Remove this

Some help? :D

Edited by MyEarth

Share this post


Link to post
Share on other sites
Jury

#include <Array.au3>

$FileOpen = FileOpen(@MyDocumentsDir & '\temp.txt', 0)
$FileRead = FileRead($FileOpen)
FileClose($FileOpen)

$aArray = StringRegExp($FileRead, '(\x22\d{2}/\d{2}/\d{4}\s\d{2}:\d{2}:\d{2}\.\d+\x22,\x22.*?\d\x22)', 3)

For $i = 0 To UBound($aArray) - 1
  ConsoleWrite($aArray[$i] & @CRLF)
Next
$aArray = _ArrayUnique($aArray)

_ArrayDisplay($aArray)

Share this post


Link to post
Share on other sites
jguinch

Or you can try this :

Local $sDate = StringRegExpReplace($data, "(?s).*?(\d{2}/\d{2}/\d{4}\h+\d{2}:\d{2}:\d{2}\.\d+)", "$1;")
Local $aDate = StringRegExp($sdate, "(\d{2}\/\d{2}\/\d{4}\h+\d{2}:\d{2}:\d{2}\.\d+);(?!.*?\1)", 3)
_ArrayDisplay($aDate)

Share this post


Link to post
Share on other sites
mikell

#include <Array.au3>

$data = '"(TEXT)","\\TEXT\TEXT\TEXT"' & @crlf & _
'"07/03/2014 10:47:55.183","0.000000"' & @crlf & _
'"07/03/2014 10:47:55.183","0.000000"' & @crlf & _
'"07/03/2014 10:48:06.216","4.122662"' & @crlf & _
'"(TEXT)","\\TEXT\TEXT\TEXT"' & @crlf & _
'"07/03/2014 10:48:18.788","1.665872"' & @crlf & _
'"07/03/2014 10:48:29.558","2.677412"' & @crlf & _
'"07/03/2014 10:48:39.112","0.199651"' & @crlf & _
'"(TEXT)","\\TEXT\TEXT\TEXT"' & @crlf & _
'"07/03/2014 10:48:39.112","0.199651"' & @crlf & _
'"07/03/2014 10:48:39.112","0.199651"'

$aArray = StringRegExp($data, '(?ms)(^"\d\d/\N+)(?!.*?\1)', 3)
_ArrayDisplay($aArray)

:)

Edited by mikell

Share this post


Link to post
Share on other sites
MyEarth

mikell, your version sometime give me "empty" string i think when was TEXTTEXTTEXT:

"07/03/2014 16:53:58.368","0.000000"
; here
"07/03/2014 16:53:59.441","0.000000"
; here
"07/03/2014 16:54:00.451","0.000000"

Any way to avoid them?

#include <Constants.au3>

Local $foo = Run('mycommand.exe', @SystemDir, @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD)
Local $data

While True
    $data &= StdoutRead($foo)
    If @error Then ExitLoop
    $aArray = StringRegExp($data, '(?ms)(^"\d\d/\N+)(?!.*?\1)', 3)
    For $i = 0 To UBound($aArray) - 1
        ConsoleWrite($aArray[$i] & @CRLF)
    Next
    Sleep(25)
WEnd
Edited by MyEarth

Share this post


Link to post
Share on other sites
mikell

Hmm, this regex should return no empty string and I can't manage to reproduce the problem - force it to return an empty string  :ermm:

Share this post


Link to post
Share on other sites
MyEarth

So i'll use the super-long-version by Juri, that don't give me any empty string.

I have always the problem of "duplicate", i don't want to do an ArrayUnique in a loop of 25ms, i have try something like:

If UBound($aArray) >= 1 Then
        $temp = $aArray[0]
        For $i = 0 To UBound($aArray) - 1
            If $temp <> $aArray[$i] Then
                ConsoleWrite($aArray[$i] & @CRLF)
                $temp = $aArray[$i]
            EndIf
        Next
    EndIf

But not work, probably the logic is wrong. Suggestion?

Edited by MyEarth

Share this post


Link to post
Share on other sites
jguinch

Did you try my regexp ?

#Include <Array.au3>

$sOutput = '"(TEXT)","\\TEXT\TEXT\TEXT"' & @CRLF & _
    '"07/03/2014 10:47:55.183","0.000000"' & @CRLF & _
    '"07/03/2014 10:48:06.216","4.122662"' & @CRLF & _
    '"(TEXT)","\\TEXT\TEXT\TEXT"' & @CRLF & _
    '"07/03/2014 10:48:18.788","1.665872"' & @CRLF & _
    '"07/03/2014 10:48:29.558","2.677412"' & @CRLF & _
    '"(TEXT)","\\TEXT\TEXT\TEXT"' & @CRLF & _
    '"07/03/2014 10:48:39.112","0.199651"' & @CRLF & _
    '"07/03/2014 10:48:39.112","0.199651"' & @CRLF & _
    '"07/03/2014 10:48:39.112","0.199651"' & @CRLF & _
    '"07/03/2014 10:48:39.112","0.199651"' & @CRLF & _
    '"07/03/2014 10:48:39.112","0.199651"' & @CRLF & _
    '"(TEXT)","\\TEXT\TEXT\TEXT"'

Local $aDate = StringRegExp($sOutput , "(?s)(\d{2}\/\d{2}\/\d{4}\h+\d{2}:\d{2}:\d{2}\.\d+)(?!.*\1)", 3)
_ArrayDisplay($aDate)

You can also delete duplicates rows with a look like this :

#Include <Array.au3>

$sOutput = '"(TEXT)","\\TEXT\TEXT\TEXT"' & @CRLF & _
    '"07/03/2014 10:47:55.183","0.000000"' & @CRLF & _
    '"07/03/2014 10:48:06.216","4.122662"' & @CRLF & _
    '"(TEXT)","\\TEXT\TEXT\TEXT"' & @CRLF & _
    '"07/03/2014 10:48:18.788","1.665872"' & @CRLF & _
    '"07/03/2014 10:48:29.558","2.677412"' & @CRLF & _
    '"(TEXT)","\\TEXT\TEXT\TEXT"' & @CRLF & _
    '"07/03/2014 10:48:39.112","0.199651"' & @CRLF & _
    '"07/03/2014 10:48:39.112","0.199651"' & @CRLF & _
    '"07/03/2014 10:48:39.112","0.199651"' & @CRLF & _
    '"07/03/2014 10:48:39.112","0.199651"' & @CRLF & _
    '"07/03/2014 10:48:39.112","0.199651"' & @CRLF & _
    '"(TEXT)","\\TEXT\TEXT\TEXT"'
    
Local $aDate = StringRegExp($sOutput, "(\d{2}\/\d{2}\/\d{4}\h+\d{2}:\d{2}:\d{2}\.\d+)", 3)
_ArrayDisplay($aDate)

$n = 0
$sTemp = ""
For $i = 0 To UBound($aDate) - 1
    If NOT StringInStr($sTemp, $aDate[$i]) Then
        $sTemp &= $aDate[$i] & ";"
        $n += 1

        $aDate[$n - 1] = $aDate[$i]
    EndIf
Next
Redim $aDate[$n]

_ArrayDisplay($aDate)

Share this post


Link to post
Share on other sites
mikell

Eventually try this in case of a N recognition problem  :ermm:

$aArray = StringRegExp($data, '(?ms)(^"\d\d/[^\r\n]+)(?!.*?\1)', 3)

or this slight adaptation of Juri's one (to check duplicates)

$aArray = StringRegExp($data, '(?s)(\x22\d{2}/\d{2}/\d{4}\s\d{2}:\d{2}:\d{2}\.\d+\x22,\x22[.\d]+\x22)(?!.*?\1)', 3)
Edited by mikell

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  

×