11 posts in this topic

guinness 1,442

$sString="ABCExample0=Return0|Example0=Return1|EXAMPLE2=Return2|Example3=Return3|Example4=Return4|"$sStringEx="ABCExample0=ABCNewReturn0|Example0=NewReturn1|Example2=NewReturn2|Example3=NewReturn3|Example4=NewReturn4|"$sString="|"&$sStringGlobal$aActualKeys[5]=["ABCExample0","Example0","Example2","Example3","Example4"]Global$aNewReturns[5]=["ABCNewReturn0","NewReturn1","NewReturn2","NewReturn3","NewReturn4"]For$i=0To4$sFind="\|"&$aActualKeys[$i]&"=.+\|"$sWrite="\|"&$aActualKeys[$i]&"="&$aNewReturns[$i]&"|"$sString=StringRegExpReplace($sString,"(?i)(?U)"&$sFind,$sWrite)Next$sString=StringTrimLeft($sString,1)ConsoleWrite("Should Be Result => "&$sStringEx&@CRLF)ConsoleWrite("Actual Result => "&$sString&@CRLF)

Share this post

Link to post

Share on other sites

Melba23 2,530

- 1: Based on your example, I assume you want to replace "Return", not "Example" - so I would suggest that:

$sFind="Example"

should read:

$sFind="Return"

- 2: You need a variable to hold the returned string!

- 3: WTF is loop doing?

- 4: Why use StringRegExpReplace anyway? You only need the much simpler StringReplace:

$sString="Example0=Return0|Example1=Return1|Example2=Return2|Example3=Return3|Example4=Return4|"$sStringEx="Example0=NewReturn0|Example1=NewReturn1|Example2=NewReturn2|Example3=NewReturn3|Example4=NewReturn4|"$sFind="Return"$sWrite="NewReturn"$sNewString=StringReplace($sString,$sFind,$sWrite); You need the new variableConsoleWrite("Should Be Result => "&$sStringEx&@CRLF)ConsoleWrite("Actual Result => "&$sNewString&@CRLF)

All clear?

M23

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort---- Sort arrays on multiple columnsChooseFileFolder---- Single and multiple selections from specified path treeview listingDate_Time_Convert-- Easily convert date/time formats, including the language usedExtMsgBox--------- A highly customisable replacement for MsgBoxGUIExtender-------- Extend and retract multiple sections within a GUIGUIFrame---------- Subdivide GUIs into many adjustable framesGUIListViewEx------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx------ Check/clear parent and child checkboxes in a TreeViewMarquee----------- Scrolling tickertape GUIsNoFocusLines------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify------------- Small notifications on the edge of the displayScrollbars----------Automatically sized scrollbars with a single commandStringSize---------- Automatically size controls to fit textToast-------------- Small GUIs which pop out of the notification area

Share this post

Link to post

Share on other sites

targeter 0

For some odd reason I think he is using the for statement to parse the five string value keys in each line, right? Though I was scratching my head at first trying to figure out why he did it like this....

- 4: Why use StringRegExpReplace anyway? You only need the much simpler StringReplace:

Because this is an INI File which is created into a string. The KeyValues are all unique but the Value might be the same in certain KeysValues e.g. "Example0=Return|Example1=Return1|Example2=Return|Example3=NewReturn3|Example4=NewReturn4|" so this is why I didn't opt for StringReplace.

Share this post

Link to post

Share on other sites

Melba23 2,530

What I posted did exactly what you asked for with your $sString and $sStringEx samples. If you do not give all the information, you cannot expect to get reasonable answers!

Do I understand that you want to replace specific values with specific new_values? If so then this might be a way to do it:

$sString="Example0=Return0|Example1=Return1|Example2=Return2|Example3=Return3|Example4=Return4|"$sStringEx="Example0=NewReturn0|Example1=NewReturn1|Example2=NewReturn2|Example3=NewReturn3|Example4=NewReturn4|"Global$aNewReturns[5]=["NewReturn0","NewReturn1","NewReturn2","NewReturn3","NewReturn4"]For$i=0To4$sFind="Example"&$i&"=.+\|"$sWrite="Example"&$i&"="&$aNewReturns[$i]&"|"$sString=StringRegExpReplace($sString,"(?U)"&$sFind,$sWrite)NextConsoleWrite("Should Be Result => "&$sStringEx&@CRLF)ConsoleWrite("Actual Result => "&$sString&@CRLF)

M23

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort---- Sort arrays on multiple columnsChooseFileFolder---- Single and multiple selections from specified path treeview listingDate_Time_Convert-- Easily convert date/time formats, including the language usedExtMsgBox--------- A highly customisable replacement for MsgBoxGUIExtender-------- Extend and retract multiple sections within a GUIGUIFrame---------- Subdivide GUIs into many adjustable framesGUIListViewEx------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx------ Check/clear parent and child checkboxes in a TreeViewMarquee----------- Scrolling tickertape GUIsNoFocusLines------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify------------- Small notifications on the edge of the displayScrollbars----------Automatically sized scrollbars with a single commandStringSize---------- Automatically size controls to fit textToast-------------- Small GUIs which pop out of the notification area

Share this post

Link to post

Share on other sites

guinness 1,442

Excellent! That is what I was looking for. I had a little play with the code and changed a few sections marked with <<<<<<. A problem I noticed is $sFind isn't case sensitive and I think "?:\A" might need to be included, but still a novice at RegExp. I tried this $sString = StringRegExpReplace($sString, "(?U)(?:\A)(?:" & $sFind, $sWrite) but it didn't seem to work correctly.

If you run following code below "ABCExample0" is changed even though it shouldn't as it doesn't match "Example0". Thanks

$sString="ABCExample0=Return0|Example1=Return1|Example2=Return2|Example3=Return3|Example4=Return4|"; <<<<<< ABCExample0 changes.Global$aNewReturns[5]=["NewReturn0","NewReturn1","NewReturn2","NewReturn3","NewReturn4"]For$i=0To4$sFind="Example"&$i&"=)(.+)(?:$|\|)"; <<<<<< Changed from "=.+\|", This means if there is a missing "|" at the end of the string it will still change the value.$sWrite="Example"&$i&"="&$aNewReturns[$i]&"|"$sString=StringRegExpReplace($sString,"(?U)(?:"&$sFind,$sWrite); <<<<<< Changed from "(?U)(?:"NextConsoleWrite("Result => "&$sString&@CRLF)

See the example below as well ABCExample0 & Example0 are both changed.

Global$sString="ABCExample0=Return0|Example0=Return1|Example2=Return2|Example3=Return3|Example4=Return4|"_Example("Example0","NewValue"); Replace the Key Example0= with a Value of NewValueFunc_Example($sValue,$sWrite="Example")$sString=StringRegExpReplace($sString,"(?U)(?:"&$sValue&"=)(.+)(?:$|\|)",$sValue&"="&$sWrite&"|")Return1EndFunc;==>_ExampleConsoleWrite($sString&@CRLF)

Share this post

Link to post

Share on other sites

guinness 1,442

This seems to work, but its a little cack-handed way of doing it. I add (?:\A|\|) and ["|" &] at the beginning of the Replace section.

$sString="Example0=Return0|Example1=Return1|Example2=Return2|Example3=Return3|Example4=Return4|"Global$aNewReturns[5]=["NewReturn0","NewReturn1","NewReturn2","NewReturn3","NewReturn4"]For$i=0To4$sFind="Example"&$i&"=)(.+)(?:$|\|)"; <<<<<< Changed from "=.+\|", This means if there is a missing "|" at the end of the string it will still change the value.$sWrite="|"&"Example"&$i&"="&$aNewReturns[$i]&"|"$sString=StringRegExpReplace($sString,"(?U)(?:\A|\|)(?:"&$sFind,$sWrite); <<<<<< Changed from ""(?U)(?:\A|\|)(?:" exact matchNextConsoleWrite("Result => "&$sString&@CRLF)

Global$sString="ABCExample0=Return0|Example0=Return1|Example2=Return2|Example3=Return3|Example4=Return4|"_Example("Example0","NewValue"); Replace the Key Example0= with a Value of NewValue_Example("ABCExample0","NewValueABC"); Replace the Key ABCExample0= with a Value of NewValueABC, but it adds "|" at the start of the string, which is expected, but not ideal.Func_Example($sValue,$sWrite="Example")$sString=StringRegExpReplace($sString,"(?U)(?:\A|\|)(?:"&$sValue&"=)(.+)(?:$|\|)","|"&$sValue&"="&$sWrite&"|")ConsoleWrite($sString&@CRLF)Return1EndFunc;==>_Example

Share this post

Link to post

Share on other sites

Melba23 2,530

I hope you will not mind me saying this, but the one thing that really gets my goat on this forum is when people keep changing the goalposts as the thread develops!

Anyway, if you make one small change (add a "|" at the beginning of your strings) this should work - for the latest iteration at least :

$sString="|ABCExample0=Return0|Example0=Return1|EXAMPLE2=Return2|Example3=Return3|Example4=Return4|"$sStringEx="|ABCExample0=ABCNewReturn0|Example0=NewReturn1|Example2=NewReturn2|Example3=NewReturn3|Example4=NewReturn4|"Global$aActualKeys[5]=["ABCExample0","Example0","Example2","Example3","Example4"]Global$aNewReturns[5]=["ABCNewReturn0","NewReturn1","NewReturn2","NewReturn3","NewReturn4"]For$i=0To4$sFind="[\A\|]"&$aActualKeys[$i]&"=.+\|"$sWrite="\|"&$aActualKeys[$i]&"="&$aNewReturns[$i]&"|"ConsoleWrite($sFind&@CRLF)ConsoleWrite($sWrite&@CRLF)$sString=StringRegExpReplace($sString,"(?i)(?U)"&$sFind,$sWrite)$Ext=@extendedConsoleWrite($Ext&@CRLF)If$Ext>2ThenExitNextConsoleWrite("Should Be Result => "&$sStringEx&@CRLF)ConsoleWrite("Actual Result => "&$sString&@CRLF)

I am afraid it is too late for me to work on an SRE for the string which does not begin with a "|" - perhaps tomorrow.

M23

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort---- Sort arrays on multiple columnsChooseFileFolder---- Single and multiple selections from specified path treeview listingDate_Time_Convert-- Easily convert date/time formats, including the language usedExtMsgBox--------- A highly customisable replacement for MsgBoxGUIExtender-------- Extend and retract multiple sections within a GUIGUIFrame---------- Subdivide GUIs into many adjustable framesGUIListViewEx------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx------ Check/clear parent and child checkboxes in a TreeViewMarquee----------- Scrolling tickertape GUIsNoFocusLines------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify------------- Small notifications on the edge of the displayScrollbars----------Automatically sized scrollbars with a single commandStringSize---------- Automatically size controls to fit textToast-------------- Small GUIs which pop out of the notification area

Share this post

Link to post

Share on other sites

guinness 1,442

Sorry, it wasn't my intention. It was complete by about post 6. I was simply asking whether I was understanding the StringRegExp values to include a case sensitive check and the "|" at the start of the string is a little cosmetic thing. I am sure StringTrim will come in handy! I don't think I have been that bad, I did give examples and tried myself.

Share this post

Link to post

Share on other sites

Melba23 2,530

Apologies, I was feeling crochety last night - comes with age, you know!

This is my final effort:

$sString="ABCExample0=Return0|Example0=Return1|EXAMPLE2=Return2|Example3=Return3|Example4=Return4|"$sStringEx="ABCExample0=ABCNewReturn0|Example0=NewReturn1|Example2=NewReturn2|Example3=NewReturn3|Example4=NewReturn4|"$sString="|"&$sStringGlobal$aActualKeys[5]=["ABCExample0","Example0","Example2","Example3","Example4"]Global$aNewReturns[5]=["ABCNewReturn0","NewReturn1","NewReturn2","NewReturn3","NewReturn4"]For$i=0To4$sFind="\|"&$aActualKeys[$i]&"=.+\|"$sWrite="\|"&$aActualKeys[$i]&"="&$aNewReturns[$i]&"|"$sString=StringRegExpReplace($sString,"(?i)(?U)"&$sFind,$sWrite)Next$sString=StringTrimLeft($sString,1)ConsoleWrite("Should Be Result => "&$sStringEx&@CRLF)ConsoleWrite("Actual Result => "&$sString&@CRLF)

I am annoyed with myself for not being able to deal with the "|" at the beginning within the SRE, but as I often say, they are probably the hardest things I have ever tried to learn in computing.

I hope that suffices for your script.

M23

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort---- Sort arrays on multiple columnsChooseFileFolder---- Single and multiple selections from specified path treeview listingDate_Time_Convert-- Easily convert date/time formats, including the language usedExtMsgBox--------- A highly customisable replacement for MsgBoxGUIExtender-------- Extend and retract multiple sections within a GUIGUIFrame---------- Subdivide GUIs into many adjustable framesGUIListViewEx------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx------ Check/clear parent and child checkboxes in a TreeViewMarquee----------- Scrolling tickertape GUIsNoFocusLines------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify------------- Small notifications on the edge of the displayScrollbars----------Automatically sized scrollbars with a single commandStringSize---------- Automatically size controls to fit textToast-------------- Small GUIs which pop out of the notification area