azkirak Posted January 9, 2018 Share Posted January 9, 2018 Hello everyone, I have the follow string: "Here is some text that should not be replaced. | this is the string that needs to be replaced | this is some ending text that should not be replaced." I would like to replace the string inside the pipes with "replacement string" and also keep the pipes for future replacements. What would be best practice for doing something like this? It is in a text field, so I am thinking I would need to copy the text field to the clipboard, make the edits, then paste it back to the text field before saving. I would assume that StringReplace andor _StringBetween could be utilized? Thanks Link to comment Share on other sites More sharing options...
Earthshine Posted January 9, 2018 Share Posted January 9, 2018 there are many ways to do a thing. do you have some code you have tried that does not work? My resources are limited. You must ask the right questions Link to comment Share on other sites More sharing options...
azkirak Posted January 9, 2018 Author Share Posted January 9, 2018 1 minute ago, Earthshine said: there are many ways to do a thing. do you have some code you have tried that does not work? Thanks for your response! I have not yet started working on this script - tomorrow morning I should have some code to work with. I was hoping someone would already have something like this they could just paste in here if it already existed or was discussed before, but my searches did not return this specific problem. I'll update this with my code tomorrow. Link to comment Share on other sites More sharing options...
Earthshine Posted January 9, 2018 Share Posted January 9, 2018 (edited) oh there are string replace wizards around here I am sure will lend a hand. Some of these people use Regular Expressions and stuff to find the stuff they want in very advanced manner meantime, check out the string functions in the help like this. this should do it for you since you are always searching between pipe delimiters | | https://www.autoitscript.com/autoit3/docs/functions/StringInStr.htm Edited January 9, 2018 by Earthshine My resources are limited. You must ask the right questions Link to comment Share on other sites More sharing options...
azkirak Posted January 9, 2018 Author Share Posted January 9, 2018 43 minutes ago, Earthshine said: oh there are string replace wizards around here I am sure will lend a hand. Some of these people use Regular Expressions and stuff to find the stuff they want in very advanced manner meantime, check out the string functions in the help like this. this should do it for you since you are always searching between pipe delimiters | | https://www.autoitscript.com/autoit3/docs/functions/StringInStr.htm Thank you, sir! I will give this a look Earthshine 1 Link to comment Share on other sites More sharing options...
czardas Posted January 9, 2018 Share Posted January 9, 2018 Another function you might find useful is _StringBetween(). Earthshine 1 operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
azkirak Posted January 9, 2018 Author Share Posted January 9, 2018 1 hour ago, czardas said: Another function you might find useful is _StringBetween(). Aren't those for arrays though? Link to comment Share on other sites More sharing options...
Earthshine Posted January 9, 2018 Share Posted January 9, 2018 (edited) if you read the help file you would know the answer to that https://www.autoitscript.com/autoit3/docs/libfunctions/_StringBetween.htm they use a local string to load to an array. you will just be searching the string after defining. Edited January 9, 2018 by Earthshine My resources are limited. You must ask the right questions Link to comment Share on other sites More sharing options...
czardas Posted January 9, 2018 Share Posted January 9, 2018 (edited) It is a string function, which expects a string as input - not an array. Because there may be several occurences within the string, an array is returned to include multiple matches between the two characters. The first match can be found at $aArray[0]. It might not be the most suitable method for your purpose, but it's good to know about and I suggest that you try the Help File example to get a better understanding of what this function does. You might need it one day. Edited January 9, 2018 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
czardas Posted January 9, 2018 Share Posted January 9, 2018 (edited) Since you are unfamiliar with Array syntax, here's an example for you to study. This does exactly what you want. Although it is possible to write this with just one line of code, you need first to understand the basics before trying more advanced methods. #include <MsgBoxConstants.au3> #include <String.au3> Local $sString = "start|original text|end" MsgBox($MB_OK, "$sString", $sString) ; Look for a string between the two characters. Local $aArray = _StringBetween($sString, '|', '|') ; Always check to see if an array was actually returned before trying to access it. If @error Then Exit ; Replace the first string that was found between the characters. Local $sNewString = StringReplace($sString, $aArray[0], "new text") ; test the result MsgBox($MB_OK, "$sNewString", $sNewString) It is for you to experiment with this and figure out how to replace text in the original string: instead of creating a new string. Edited January 9, 2018 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
orbs Posted January 9, 2018 Share Posted January 9, 2018 (edited) why bother with _StringBetween() when StringReplace() alone can do the trick? #include <MsgBoxConstants.au3> #include <String.au3> Local $sString = "start|original text|end" MsgBox($MB_OK, 'original string', $sString) $sString = StringReplace($sString, '|original text|', '|new text|') MsgBox($MB_OK, 'new string', $sString) note: i opted to replace the string including the pipe delimiters, to avoid accidental replacements. Edited January 9, 2018 by orbs Signature - my forum contributions: Spoiler UDF: LFN - support for long file names (over 260 characters) InputImpose - impose valid characters in an input control TimeConvert - convert UTC to/from local time and/or reformat the string representation AMF - accept multiple files from Windows Explorer context menu DateDuration - literal description of the difference between given dates Apps: Touch - set the "modified" timestamp of a file to current time Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes SPDiff - Single-Pane Text Diff Link to comment Share on other sites More sharing options...
farouk12 Posted January 9, 2018 Share Posted January 9, 2018 (edited) Why not using StringRegExpReplace ? Edited January 9, 2018 by farouk12 Link to comment Share on other sites More sharing options...
iamtheky Posted January 10, 2018 Share Posted January 10, 2018 (edited) 3 hours ago, orbs said: why bother with _StringBetween() when StringReplace() alone can do the trick? because the OP obv lacks fundamentals and taking the UDF route they learn about arrays? I'd imagine all *string*replace* function recommendations would help exactly the same. Except this, this one might be a brutal first take: Local $sString = "start|original text|end" $delim = "|" $newstring = "New Text" $sReplace = StringReplace($sString , stringmid($sString , stringinstr($sString , $delim) , StringInStr($sString , $delim , 0 , 2) - stringinstr($sString , $delim) + 1) , $delim & $newstring & $delim) msgbox(0, '' , $sReplace) Edited January 10, 2018 by iamtheky czardas and kylomas 2 ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
iamtheky Posted January 10, 2018 Share Posted January 10, 2018 Or this one, that retains the pipes Local $sString = "start|original text|end" $NewString = "New Text" msgbox(0 , "" , StringLeft($sString , StringInStr($sString , "|")) & $NewString & StringRight($sString , StringInStr(stringreverse($sString) , "|"))) ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
czardas Posted January 10, 2018 Share Posted January 10, 2018 (edited) 15 hours ago, orbs said: why bother with _StringBetween() when StringReplace() alone can do the trick? I assumed the string to be replaced was unknown. Otherwise I would agree with you. Edited January 10, 2018 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
czardas Posted January 10, 2018 Share Posted January 10, 2018 (edited) 13 hours ago, farouk12 said: Why not using StringRegExpReplace ? Of course you can - if you know how. Edited January 10, 2018 by czardas Earthshine 1 operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
Earthshine Posted January 10, 2018 Share Posted January 10, 2018 (edited) Didn’t I say there were string experts that use regular expressions? Edited January 10, 2018 by Earthshine My resources are limited. You must ask the right questions Link to comment Share on other sites More sharing options...
czardas Posted January 10, 2018 Share Posted January 10, 2018 For example: MsgBox(0, "", StringRegExpReplace('start|original text|end', '(\|.*\|)', '|new text|')) operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
ViciousXUSMC Posted January 10, 2018 Share Posted January 10, 2018 This is one time I feel RegEx is better than the string functions, is shorter, and does exactly what is needed without a complicated pattern. czardas version works well, it does strip the pipes and have you add them back in the new text is the only thing I can see to be aware of. czardas and Earthshine 2 Link to comment Share on other sites More sharing options...
genius257 Posted January 10, 2018 Share Posted January 10, 2018 38 minutes ago, czardas said: For example: MsgBox(0, "", StringRegExpReplace('start|original text|end', '(\|.*\|)', '|new text|')) Nice, but on the odd chance the string contains a newline sequence it won't work without (?s) My suggestion: '(\|[^|]*\|)' Earthshine 1 My highlighted topics: AutoIt Package Manager, AutoItObject Pure AutoIt, AutoIt extension for Visual Studio Code Github: AutoIt HTTP Server, AutoIt HTML Parser 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