Jump to content

string parsing regex question


quickies
 Share

Recommended Posts

 ini config file holds values formatted like so

@keyName=@HOUR:@MIN.@SEC.@MSEC

Only the values not the key names need to be replaced with their true values. Assuming iniread function has been processed at this point, $var[0][0]. Only $var[$x][1] needs to be handled.

I am currently using a function that runs through a regex expression and replaces the data one expression at a time but this is redundant.

StringRegExpReplace($ret, '(?i)\@HOUR\b', @HOUR)
StringRegExpReplace($ret, '(?i)\@MIN\b', @MIN)

I tried "@HOUR|@MIN|@SEC" expression run through regexreplace loop with no success. I end up being able to replace values one at a time but then I do one of these &= and just end up with appended strings on top of the previous return.

Finally the return values are put into an array. I don't mind a 2 dimensional solutions in the response for this part. I am currently just using 1 dimension with [0] as number of values [1] = @keyname [2] = values, and just skipping every other when I need the return. I'm sure [0][0] is much more efficient. I just personally don't like looking at nested arrays 

The solutions final return should be

@keyname=12:12.12.256

Of course the 12:12.12.256 is just an example representation of @HOUR:@MIN and so on not to be returned literally.

This is the general idea to the function that I am trying to build. Hopefully I have been a descriptive as possible. Thank you.

Global $macroArr[9] = [8, _
'@XTIME',   @HOUR&':'&@MIN&'.'&@SEC&'.'&@MSEC, _
'@XDATE',   $WDAY&' '&@MON&'.'&@MDAY&'.'&@YEAR, _
'@XTIMEXT', $XTIMEXT, _
'@XDATEXT', $XDATEXT _
]

; pseudo for $x IniReadSection loop here
; $iniVal[$y][0] = @XTIME ....
    $retStr = _func($iniVal[$y][1]) ; ini loop sent a string like @HOUR:@MIN.@SEC.@MSEC
    consoleWrite($retStr) ; which again should be something like 12:12.12.256
    ; if I can get to this point I can take it from here
    ;@XTIME=12:12.12.256
; next


Func _func($iniParam) ; another For $x loop through the $iniParam is needed here this is just pseudo code
    For $i = 1 to Ubound($macroArr) - 1
        If $i = $macroArr[0] - 1 then
            $ret = StringRegExpReplace($iniParam[$x][1], '(?i)\'&$macroArr[$i]&'\b', $macroArr[$i+1])
            ExitLoop 1
        Else
            $ret = StringRegExpReplace($iniParam[$x][1], '(?i)\'&$macroArr[$i]&'\b', $macroArr[$i+1])
        EndIf
    Next
    Return($ret)
EndFunc
Edited by quickies
Link to comment
Share on other sites

Not sure I understood what you exactly want, something like this ?

$str = "@keyName=@HOUR:@MIN.@SEC.@MSEC"

$res = Execute(StringRegExpReplace($str, '^([^=]+)=(@\w+):(@\w+).(@\w+).(@\w+)', _ 
        "'$1=' & Execute('$2') & ':' & Execute('$3') & '.' & Execute('$4') & '.' & Execute('$5') & ") & "''")

Msgbox(0,"", $res)

or this for an array output

Local $var[2]
$var[0] = "@keyName"
$var[1] = "@HOUR:@MIN.@SEC.@MSEC"

$res = Execute(StringRegExpReplace($var[1], '(@\w+):(@\w+).(@\w+).(@\w+)', _ 
        "Execute('$1') & ':' & Execute('$2') & '.' & Execute('$3') & '.' & Execute('$4') & ") & "''")

Msgbox(0,"", $var[0] & "=" & $res)
Edited by mikell
Link to comment
Share on other sites

Why don't you simply update value of the key in the appropriate section?

Something like:

IniWrite ( @ScriptDir & "\MyIni.ini", "Time", "@keyName", @HOUR & ":" & @MIN & "." & @SEC & "." & @MSEC )

Br,

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Here is another possible solution.

#include <Array.au3>
#include <Date.au3>

Local $res
Local $str = "@keyName=@HOUR:@MIN.@SEC.@MSEC" & @CRLF & _
             "@keyName1=Something else" & @CRLF & _
             "@keyName2=@HOUR:@MIN:@SEC.@MSEC"
Local $tTime = _Date_Time_GetLocalTime()
Local $aTime = _Date_Time_SystemTimeToArray($tTime)
;_ArrayDisplay($aTime)

$res = StringRegExpReplace($str, '(@\w+)=(@\w+)(.)(@\w+)(.)(@\w+).(@\w+)', _
        StringFormat('$1=%02s${3}%02s${5}%02s.%03s', $aTime[3], $aTime[4], $aTime[5], $aTime[6]))

MsgBox(0, "Results", $res)
Link to comment
Share on other sites

I have a few functions that run through ini files and allow users to define and customize macros variables whatever.

Script follows this process

Function _LoadConfiguration

Func _LoadConfiguration($iniret)
For $x = 0 to ubound($iniret) - 1
    Local $ret2 = IniReadSection($iniconfigfile, $iniret[$x])
    Switch $iniret[$x]

        Case 'MacroControls'
                _Macros($ret2)  

        Case 'SystemSettings'
            For $y = 1 To UBound($ret2) - 1
                Switch $ret2[$y][0]
                        Case 'time-format'
                        $tformat = _Macros($ret2[$y][1])
                    
                    Case 'date-format'
                        $dformat = _Macros($ret2[$y][1])
                                EndSwitch
                        Next

        EndSwitch
Next
EndFunc

Function _Macros

Func _Macros($inParam)
    ; if array then contains 2D [0][0] ini key=value pairs
        ; was passed from _Macro()
    If IsArray($inParam) = 1 then
        ; ??????????????????????????????????????????????????
        ; ??????????????????????????????????????????????????
        EndIf
        Return $ret
EndFunc

The ini file is set up like so

[MacroControls]
@XTIME=@HOUR:@MIN.@SEC.@MSEC
@XDATE=$WDAY @MON.@MDAY.@YEAR
@XTIMEXT=$XTIMEXT
@XDATEXT=$XDATEXT


[SystemSettings]
time-format=@XTIME
date-format=@XDATEXT

The idea is to let the user define the @macro names, values and then be able to use them in other areas of the ini file. If you remove the case _MacroControls bit and add this to the _Macro fucntion you can see what I am looking for. You will of course have to define $WDAY, $XTIMEXT.....and any other variables with some default data as well. In the end when the ini parser reaches section SystemSettings key time-format it knows @XTIME = whatever the MacroControls section set it to.

Global $macroArr[9] = [8, '@XTIME',   @HOUR&':'&@MIN&'.'&@SEC&'.'&@MSEC, _
                              '@XDATE',   $WDAY&' '&@MON&'.'&@MDAY&'.'&@YEAR, _
                              '@XTIMEXT', $XTIMEXT, _
                              '@XDATEXT', $XDATEXT _
    ]
    For $i = 1 to Ubound($macroArr) - 1
        If $i = $macroArr[0] - 1 then
            $ret = StringRegExpReplace($inParam, '(?i)\'&$macroArr[$i]&'\b', $macroArr[$i+1])
            ExitLoop 1
        Else
            $ret = StringRegExpReplace($inParam, '(?i)\'&$macroArr[$i]&'\b', $macroArr[$i+1])
        EndIf
    Next

As of right now inside the _Macro function I was fiddling around with thought to bring promise, forgetting that Execute() will do nothing for $someVar, not being an expression. oops The delimiters that may be used in the config file can change based on the users preferences.

The values may or may not include some sort of $variable or @macro reference that is user defined. When you pull the value and it consist of @something when returned will produce string @something because iniread functions return parameters as strings which is the way it should be since ini is not au3. I am attempting to stay away from a parser internally having to process this. I was thinking a regex find and replace storing the values into an array as stated previous and calling upon them when needed would be a better approach but could be wrong. I would think though given the above sample produces the result I am looking for the part that is needed now is defining the data from the ini rather than script

If IsArray($inParam) = 1 then
        Local $tmpRet = ''
        Local $tmpArr[1]
        For $x = 1 to $inParam[0][0]
            $ret = StringRegExp($inParam[$x][1], _
            '(?i)\@HOUR|@MIN|@SEC|@MSEC|@MON|@MDAY|@YEAR|$WDAY|$XTIMEXT|$XDATEXT\b', 3)
            
            For $y = 0 to Ubound($ret) - 1
                $retExec = Execute($ret[$y])
                $tmpRet &= $retExec
            Next
            
            _ArrayInsert($tmpArr, 0, ($x)*2)
            _ArrayDelete($tmpArr, 1)
            _ArrayAdd($tmpArr, $inParam[$x][0])
            _ArrayAdd($tmpArr, $tmpRet)
            $tmpRet = ''
        Next
        

        For $z = 1 to $tmpArr[0]
            msgbox(0, '', $tmpArr[$z])
        Next
        
    EndIf

 

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...