youtuber Posted July 6, 2023 Share Posted July 6, 2023 (edited) 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 July 6, 2023 by youtuber Link to comment Share on other sites More sharing options...
Andreik Posted July 6, 2023 Share Posted July 6, 2023 (edited) 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 July 6, 2023 by Andreik youtuber 1 Link to comment Share on other sites More sharing options...
youtuber Posted July 6, 2023 Author Share Posted July 6, 2023 (edited) 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 July 6, 2023 by youtuber Link to comment Share on other sites More sharing options...
Andreik Posted July 6, 2023 Share Posted July 6, 2023 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) youtuber 1 Link to comment Share on other sites More sharing options...
pixelsearch Posted July 6, 2023 Share Posted July 6, 2023 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) youtuber 1 Link to comment Share on other sites More sharing options...
youtuber Posted July 6, 2023 Author Share Posted July 6, 2023 (edited) @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 July 6, 2023 by youtuber Link to comment Share on other sites More sharing options...
pixelsearch Posted July 6, 2023 Share Posted July 6, 2023 I use it that way in my script Regexp Quicktester where saving the pattern in an ini file has to be done "as-is", all characters in the pattern being crucial (spaces, quotes etc...) youtuber 1 Link to comment Share on other sites More sharing options...
Andreik Posted July 6, 2023 Share Posted July 6, 2023 @youtuber this is why I told you to run the regex multiple times until replaces all unwanted leading and trailing characters. Saving as binary is at least odd since you will basically double the size of data in ini file. youtuber 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now