Jump to content

Best practice for replacing string between specific characters


Recommended Posts

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

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

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 by Earthshine

My resources are limited. You must ask the right questions

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 by czardas
Link to comment
Share on other sites

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 by czardas
Link to comment
Share on other sites

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

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 by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

Or this one, that retains the pipes :evil:

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

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: '(\|[^|]*\|)'

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...