Jump to content

Recommended Posts

Posted

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

Posted
  On 1/9/2018 at 4:15 PM, Earthshine said:

there are many ways to do a thing. do you have some code you have tried that does not work?

Expand  

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.

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

My resources are limited. You must ask the right questions

 

Posted
  On 1/9/2018 at 4:20 PM, 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

Expand  

Thank you, sir!  I will give this a look

Posted (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 by czardas
Posted (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 by czardas
Posted (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 by orbs

Signature - my forum contributions:

  Reveal hidden contents

 

Posted (edited)
  On 1/9/2018 at 8:43 PM, orbs said:

why bother with _StringBetween() when StringReplace() alone can do the trick?

Expand  

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

  Reveal hidden contents

Posted

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) , "|")))

 

  Reveal hidden contents

Posted (edited)

 Didn’t I say there were string experts that use regular expressions? 

Edited by Earthshine

My resources are limited. You must ask the right questions

 

Posted

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.

Posted
  On 1/10/2018 at 12:59 PM, czardas said:

For example:

MsgBox(0, "", StringRegExpReplace('start|original text|end', '(\|.*\|)', '|new text|'))

 

Expand  

Nice, but on the odd chance the string contains a newline sequence it won't work without (?s)

My suggestion: '(\|[^|]*\|)'

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...