Sign in to follow this  
Followers 0
lokster

Need RegEx to parse key=value pairs if the "value" is multiline

9 posts in this topic

I need RegEx to parse INI file in the following format

key1=line1 \
line2 \
line3

(like in the Scite config files)

after parsing, the value must be "line1 line2 line3"

I need this, because I have INI file with values that are very loooong and cause the OS to hang if I try to open them in some external text editor to edit them

Share this post


Link to post
Share on other sites



Check out the functions IniReadSectionNames and IniRead.


[quote name='Valik' post='301213' date='Jan 31 2007, 10:36 PM']You seem to have a habit of putting things in the wrong place. I feel sorry for any female you attempt to have sex with.[/quote][font="Lucida Sans Unicode"][/font]

Share this post


Link to post
Share on other sites

HI,

I like to play with RegExp. Show the file. and an example how it should look after RegExp or what you need.

So long,

Mega


Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Share this post


Link to post
Share on other sites

here is example (this is part of the real file)

[style]
words0=and byref case const continuecase continueloop \
default dim do else elseif endfunc endif endselect endswitch \
endwith enum exit exitloop false for func global if in local \
next not or redim return select step switch then to true until \
wend while with

I need to read the "words0" key in autoit, and the resulting string must be:

"and byref case const continuecase continueloop default dim do else elseif endfunc endif endselect endswitch endwith enum exit exitloop false for func global if in local next not or redim return select step switch then to true until wend while with "

(only one line, without the "\" characters)

There is more than one key in this format in the ini file (words1=...,words2=...). And NO, the IniRead() is no good for me.

I think the regex will do the work perfect, but I am not very good with regex'es...

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

here is example (this is part of the real file)

[style]
words0=and byref case const continuecase continueloop \
default dim do else elseif endfunc endif endselect endswitch \
endwith enum exit exitloop false for func global if in local \
next not or redim return select step switch then to true until \
wend while with

I need to read the "words0" key in autoit, and the resulting string must be:

"and byref case const continuecase continueloop default dim do else elseif endfunc endif endselect endswitch endwith enum exit exitloop false for func global if in local next not or redim return select step switch then to true until wend while with "

(only one line, without the "\" characters)

There is more than one key in this format in the ini file (words1=...,words2=...). And NO, the IniRead() is no good for me.

I think the regex will do the work perfect, but I am not very good with regex'es...

There's already functions for this as mentioned up top. IniReadSection will return the Key and the Value in a 2 dimensional array... [n][0] holds the key, and [n][1] holds the value of that key.

If the above function isn't good, then explain why it isn't good.

Edit:

It just takes molding the function to suit your needs I think:

Func _IniSplitVal($hFile, $sSection, $vDelim)
    Local $aSec = IniReadSection($hFile, $sSection)
    If IsArray($aSec) = 0 Then Return SetError(1, 0, '')
    Local $aReturn[UBound($aSec)][2], $aSplit
    For $iCC = 1 To UBound($aSec) - 1
        $aReturn[$iCC][0] = $aSec[$iCC][0]
        If StringInStr($aSec[$iCC][1], $vDelim) Then
            $aSplit = StringSplit($aSec[$iCC][1], $vDelim)
            $aReturn[$iCC][1] = $aSplit[1]
        Else
            $aReturn[$iCC][1] = $aSec[$iCC][1]
        EndIf
    Next
    Return $aReturn
EndFunc
Edited by SmOke_N

[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

@SmOke_N

Your function does not work for me. As I said before, there are multiple multiline key=value pairs in my ini. There is only one section. Your function can't read the value of specific key.

Nevermind, I created a function that does the work for me.

And it's without regular expression. It's similar to IniRead(), but it can read multiline values.

If you want to read key named "key1" and its value is split in multiple lines like this:

[section1]
key1=this is 
supposed 
to be a long 
line

key2=this is 
another long 
long long line....

the func will read the file line by line until it finds the string "key1=". Then it appends in one string everything between "key1=" and the next blank line, "key=value" pair, or "\" char.

And thus, the resulting string will be "this is supposed to be a long line"

The function seraches for the first occurance of "key1", because in my ini there is only one section with unique key names.

Maybe it can be done better... but for now, it works:)

Func IniReadLong($file,$key,$default="")
    $handle = FileOpen($file,0)
    Local $longline
    Local $found = False
    while 1
        $line = FileReadLine($handle)
        if @error = -1 Then
            ExitLoop
        EndIf

        $pos = StringInStr($line,$key&"=",0,1)
        
        if $pos = 1 Then
            $found = true
            $line = StringReplace($line,$key&"=","",1)
        EndIf
        
        if $found Then
            if $line = "" Then
                ExitLoop
            ElseIf StringInStr($line,"\",0,-1)>0 Then
                $line = StringTrimRight($line,1)
                $longline &= $line
                ExitLoop
            ElseIf StringInStr($line,"=")>0 Then
                ExitLoop
            Else
                $longline &= $line
            EndIf
        EndIf

    WEnd
    FileClose($handle)
    if $longline = "" Then
        $longline = $default
    EndIf
    Return $longline
EndFunc

Share this post


Link to post
Share on other sites

So you are only trying to remove the slashes? I'm so confused... Are you trying to read the au3.keywords.properties?


[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

@lokster, although you have created a working UDF it is a perfect situation to explore StringRegExp and possibly time it against your own UDF.

I hate to say I can't do testing now. But this is the start. I might get some time later this evening to refine the pattern if none else does it.:)

Local $data = FileRead($filename)
Local $pattern = "word\d([\w ]*)" ;Can't remember if \w includes spaces
$res = StringRegExp($data, $pattern, 3)

Share this post


Link to post
Share on other sites

@lokster, although you have created a working UDF it is a perfect situation to explore StringRegExp and possibly time it against your own UDF.

I hate to say I can't do testing now. But this is the start. I might get some time later this evening to refine the pattern if none else does it.:)

Local $data = FileRead($filename)
Local $pattern = "word\d([\w ]*)" ;Can't remember if \w includes spaces
$res = StringRegExp($data, $pattern, 3)oÝ÷ Ûú®¢×v'gßÛ[¢Ø^¯­$Dø^ËkÊ)à¶h¶©®^®Ø±ø¥xéÝz»-jwp(^¶§Â'uû§rبƫzz-§Ú0¢¹¢¸b·jëÞ¡ûazhZ½æ§iËnj[rبò¢èZ½æ§ªê-x¬zÛ©ªê-nëg¢Ç¶*'¶¬Â)eµ©qªÞ¡ûajج²çÛazÊ^yج~)ÞjÌV®¶­sbb33c¶Òôæ7ÆEfÂ&öw&ÔfÆW4F"fײgV÷C²b3#´WFôC2b3#µ66DRb3#´FVg2b3#µ&öGV7Föâb3#¶S2æ¶Wv÷&G2ç&÷W'FW2gV÷C²Âb33´ç6V7Föâb33²Âb33²b3#²b33²¤b4'&b33c¶FVà 6öç6öÆUw&FR5$Äb f÷"b33c¶42ÒFòT&÷VæBb33c¶Ò 6öç6öÆUw&FRb33´¶WÒb33²fײb33c¶²b33c¶45Õ³Òfײ5$Äbfײb33µfÇVW3¢b33²fײ5$Äbfײb33c¶²b33c¶45Õ³Òfײ5$Äb æW@¤VæD` ¤gVæ2ôæ7ÆEfÂb33c¶fÆRÂb33c·56V7FöâÂb33c·dFVÆÒ Æö6Âb33c·5&VBÒfÆU&VBb33c¶fÆRÂb33c¶6V2Âb33c·5FV×Òb33c·5&V@ b33c·5&VBÒb33µ²b33²fײb33c·56V7Föâfײb33µÒb33²fײ5$Äbfײb33c·5&V@ fÆUw&FRFV×F"fײb33²b3#´×æ&VE6V7FöåF×çF×b33²Âb33c·5&VB b33c¶6V2Òæ&VE6V7FöâFV×F"fײb33²b3#´×æ&VE6V7FöåF×çF×b33²Âb33c·56V7Föâ fÆTFVÆWFRFV×F"fײb33²b3#´×æ&VE6V7FöåF×çF×b33² b4'&b33c¶6V2ÒFVâ&WGW&â6WDW'&÷"ÂÂb33²b33² Æö6Âb33c¶&WGW&åµT&÷VæBb33c¶6V2Õ³%ÒÂb33c·4öÆ@ b33c·5FV×Ò7G&æu&WÆ6R7G&æu&WÆ6R7G&æu7G&u2b33c·5FV×ÂBÂÄbÂb33²b33²Â5"Âb33²b33² f÷"b33c¶42ÒFòT&÷VæBb33c¶6V2Ò b33c·5FV×Ò7G&æu&WÆ6Rb33c·5FV×Âb33c¶6V5²b33c¶45Õ³ÒÂ6"fײb33c¶6V5²b33c¶45Õ³Ò æW@ Æö6Âb33c¶7ÆBÒ7G&æu7ÆBb33c·5FV×Â6" f÷"b33c¶42ÒFòT&÷VæBb33c¶7ÆBÓ b33c·4öÆBf׳Òb33c¶7ÆE²b33c¶45Òfײ5$Ä` æW@ b33c·4öÆBÒ7G&æu&WÆ6Rb33c·4öÆBÂb33c·dFVÆÒÂb33²b33² b33c·4öÆBÒb33µ²b33²fײb33c·56V7Föâfײb33µÒb33²fײ5$Äbfײb33c·4öÆ@ fÆUw&FRFV×F"fײb33²b3#´×æ&VE6V7FöåF×çF×b33²Â7G&æuG&Õ&vBb33c·4öÆBÂ" b33c¶6V2Òæ&VE6V7FöâFV×F"fײb33²b3#´×æ&VE6V7FöåF×çF×b33²Âb33c·56V7Föâ fÆTFVÆWFRFV×F"fײb33²b3#´×æ&VE6V7FöåF×çF×b33² b4'&b33c¶6V2ÒFVâ&WGW&â6WDW'&÷""ÂÂb33²b33² &WGW&âb33c¶6V0¤VæDgVæ0

[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

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  
Followers 0