Jump to content

RegExpReplace control when writing to ini file?


Recommended Posts

Is there a quotation mark and a space at the beginning and end when writing in an INI file?
I want to check it using RegExpReplace. If there are spaces and quotation marks at the beginning and end, I want to add them. Then, I want to write it to the INI file.
I am sure that my regex pattern is working, but I don't know how to use it with StringRegExpReplace.

#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

$aDirPro = @ScriptDir & "\IniFolder"
If FileExists($aDirPro) = 0 Then DirCreate($aDirPro)
$SectionName = "Values"
$Form1 = GUICreate("Form", 500, 200)
$valuesini = GUICtrlCreateInput("", 16, 67, 473, 25)
$ButtonSaveIni = GUICtrlCreateButton("Save", 16, 100, 475, 70)
GUISetState(@SW_SHOW)
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

        Case $ButtonSaveIni
            $ReadComboLoadValue = GUICtrlRead($valuesini)
            $RegExpRepSpaceAndQuotesControl = StringRegExpReplace($ReadComboLoadValue, '^\"|\"$', '$1"$2"')
            $RegExpRepSpaceAndQuotesControl = StringRegExpReplace($ReadComboLoadValue, '^\s|\s$', '$1"$2"')
            $Ini_FileNameReadCombo = $aDirPro & '\' & "IniFile" & '.ini'
            If Not GUICtrlRead($valuesini) = "" Then
                IniWrite($Ini_FileNameReadCombo, $SectionName, 'Value', $RegExpRepSpaceAndQuotesControl)
                GUICtrlSetData($valuesini, "")
                GUICtrlSetData($valuesini, IniRead($Ini_FileNameReadCombo, $SectionName, 'Value', ''))
                MsgBox(0, "Saved Ok.", "Saved Ok.", 3)
            Else
                MsgBox(48, "Incorrect operation!", "Cannot be empty...", 5)
                GUICtrlSetState($valuesini, $GUI_FOCUS)
            EndIf

    EndSwitch
WEnd

 

Edited by youtuber
Link to comment
Share on other sites

You need to formulate your questions way better. So you want to check if a string begin and end with double quotes or space. If the pattern match do you still want to add double quotes and spaces? What if the string start with space and end with double quotes?

Edited by Andreik

When the words fail... music speaks.

Link to comment
Share on other sites

IniRead ignores spaces and quotes if the values written in the ini file start and end with a space and start with a double quote and end with a double quote, I want to avoid this before writing to the ini file.

For example, if the user makes an regex input to the input as in the following example.

$valuesini = GUICtrlCreateInput('","event":"(.*?)"timing":"', 16, 67, 473, 25)

I want it to be written to the ini file like this

[Values]
Value="","event":"(.*?)"timing":""

 

Edited by youtuber
Link to comment
Share on other sites

This will replace leading and trailing double quotes and spaces.

$sString = '","event":"(.*?)"timing":"'

$sString = StringRegExpReplace($sString, '^[" ]|[" ]$', '') & @CRLF
ConsoleWrite($sString)

But maybe you should test the pattern multiple times since you might have a string like that:

Quote

","event":"(.*?)"timing":      "

So you replace the trailing double quotes but you end up with a trailing space. You can prevent that by testing the pattern multiple times.

$sString = '","event":"(.*?)"timing":      "'

Do
    $sString = StringRegExpReplace($sString, '^[" ]|[" ]$', '')
Until @extended = 0

ConsoleWrite($sString & @CRLF)

 

When the words fail... music speaks.

Link to comment
Share on other sites

Just in case... if you're not bothered your ini file isn't easily readable outside the script, then you can iniwrite/iniread it like this, without any space or double quotes issues. Even multi lines will be accepted :

IniWrite($sIniFile, "Main", "Pattern", StringToBinary(GUICtrlRead($ebRegExp), 4)) ; 4 = $SB_UTF8 (string data is UTF8)
...
GUICtrlSetData($ebRegExp, BinaryToString(IniRead($sIniFile, "Main", "Pattern", ""), 4)) ; 4 = $SB_UTF8 (binary data is UTF8)

Link to comment
Share on other sites

@pixelsearch thanks, Your suggestion is not suitable for me as I want the user to make manual changes to the ini file.
I solved this works fine for me.

$RegExpRepSpaceAndQuotesControl = StringRegExpReplace($ReadComboLoadValue, '^\"|\"$', '"$1$2"')

$RegExpRepSpaceAndQuotesControl = StringRegExpReplace($ReadComboLoadValue, '^\s|\s$', '$1 " $2')
;or
;$RegExpRepSpaceAndQuotesControl = StringRegExpReplace($ReadComboLoadValue, '^\s|\s$', ' $1"$2 ')

 

Edit: The problem continues

@pixelsearch I think you're right, I'll have to use your suggestion, it doesn't seem to have an end, if there are quotes with spaces, it still won't work.

sample

$valuesini = GUICtrlCreateInput('" ","event":"(.*?)"timing":" "', 16, 67, 473, 25)

Edited by youtuber
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...