stringreplace
Moderators: JRL, Dorian (MJT support)
stringreplace
I am trying to remove some XML tags from a string so my code looks like this
StringReplace>username,,,result1
StringReplace>result1,, ,result2
let>username=%result2%
I noted a couple of things -
1. Stringreplace does not seem to work on multiple finds ie does not replace each find within the string , it only does one, hence the multiple replaces.
2. The replace with Null seems not to work if I leave nothing between the replace commas, I get a zero inserted
e.g.
USERNAME= 0ADMIN
RESULT2= 0ADMIN
RESULT1= 0ADMIN
StringReplace>username,,,result1
StringReplace>result1,, ,result2
let>username=%result2%
I noted a couple of things -
1. Stringreplace does not seem to work on multiple finds ie does not replace each find within the string , it only does one, hence the multiple replaces.
2. The replace with Null seems not to work if I leave nothing between the replace commas, I get a zero inserted
e.g.
USERNAME= 0ADMIN
RESULT2= 0ADMIN
RESULT1= 0ADMIN
- Bob Hansen
- Automation Wizard
- Posts: 2475
- Joined: Tue Sep 24, 2002 3:47 am
- Location: Salem, New Hampshire, US
- Contact:
It would be easier for us to help if you provided some examples of the original XML strings. But using "null" as a replacement works fine with StringReplace.
The format for StringReplace is:
StringReplace>haystack,needle,newneedle,newhaystack
This will replace all needles found in the haystack with new needles, and put the final result into a new haystack. (You can actually put the result in the same haystack if you want.
But, and are not the same needles, that is why you need muliple StringReplace commands, one for each distinct needle.
You could use VB RegEx command to replace both strings though. RegEx allows you to work with wild cards, but you will need to learn the syntax for Regular Expressions.
Getting familiar with RegEx is beneficial. In the sample you are showingvthough, the simple two StringReplace lines are adequate and shorter. But using RegEx has the possibility of removing all "" expressions in one pass. Searching for all groups of characters surrounded by "" and replacing them with nothing. Regular expressions takes some effort to learn and lots of practice to get right, so don't expect it to be a quick fix. Here is a link showing part of a three plus hours learning curve I had with a simple phrase: http://www.mjtnet.com/forum/viewtopic.p ... ight=regex
Search this forum for RegEx, and you will find many examples. Here is a sample from Marcus of using RegEx to remove multiple spaces: http://www.mjtnet.com/forum/viewtopic.p ... ight=regex
The format for StringReplace is:
StringReplace>haystack,needle,newneedle,newhaystack
This will replace all needles found in the haystack with new needles, and put the final result into a new haystack. (You can actually put the result in the same haystack if you want.
But, and are not the same needles, that is why you need muliple StringReplace commands, one for each distinct needle.
You could use VB RegEx command to replace both strings though. RegEx allows you to work with wild cards, but you will need to learn the syntax for Regular Expressions.
Getting familiar with RegEx is beneficial. In the sample you are showingvthough, the simple two StringReplace lines are adequate and shorter. But using RegEx has the possibility of removing all "" expressions in one pass. Searching for all groups of characters surrounded by "" and replacing them with nothing. Regular expressions takes some effort to learn and lots of practice to get right, so don't expect it to be a quick fix. Here is a link showing part of a three plus hours learning curve I had with a simple phrase: http://www.mjtnet.com/forum/viewtopic.p ... ight=regex
Search this forum for RegEx, and you will find many examples. Here is a sample from Marcus of using RegEx to remove multiple spaces: http://www.mjtnet.com/forum/viewtopic.p ... ight=regex
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!
Bob
A humble man and PROUD of it!
Thanks
Bob
Thank you, however originally I tried
StringReplace>user,UserLogon,,user
The result was it only replaced one instance for each code line
It also left a zero instead of a null
The XML is read in from a file. Essentially the line to change is let>user=Ian
The result of the instruction above is
user=Ian
Thank you, however originally I tried
StringReplace>user,UserLogon,,user
The result was it only replaced one instance for each code line
It also left a zero instead of a null
The XML is read in from a file. Essentially the line to change is let>user=Ian
The result of the instruction above is
user=Ian
- Marcus Tettmar
- Site Admin
- Posts: 7395
- Joined: Thu Sep 19, 2002 3:00 pm
- Location: Dorset, UK
- Contact:
Try the following:
Let>user=Ian
StringReplace>user,UserName,0,user
MessageModal>user
It produces "Ian"
StringReplace replaces ALL occurrences, as can be seen above.
Let>user=Ian
StringReplace>user,UserName,0,user
MessageModal>user
It produces "Ian"
StringReplace replaces ALL occurrences, as can be seen above.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
String Replace
Marcus
Thanks for your response, yes you are quite correct in your example which I have run with success, however in my application I start off with username having the following in it
ADMIN
// note the spaces in the original
when I run the following code
StringReplace>username,,,username
I get in the debugger
USERNAME= 0ADMIN
which is quite different and most frustrating, I don't want the zeroes and the fact it replaces once is only a small issue that I can live with. The bigger issue is the failure to delete which leaves the zero.
Version 10.1.19
Thanks for your response, yes you are quite correct in your example which I have run with success, however in my application I start off with username having the following in it
ADMIN
// note the spaces in the original
when I run the following code
StringReplace>username,,,username
I get in the debugger
USERNAME= 0ADMIN
which is quite different and most frustrating, I don't want the zeroes and the fact it replaces once is only a small issue that I can live with. The bigger issue is the failure to delete which leaves the zero.
Version 10.1.19
- Marcus Tettmar
- Site Admin
- Posts: 7395
- Joined: Thu Sep 19, 2002 3:00 pm
- Location: Dorset, UK
- Contact:
DOES only appear once in that string.
Somewhere in your script you must have - inadvertently - set empty string (nul) to zero.
Only way to help you is debug your script. If you can't/don't want to do that yourself, post your full script here so that we have a chance to help you.
Somewhere in your script you must have - inadvertently - set empty string (nul) to zero.
Only way to help you is debug your script. If you can't/don't want to do that yourself, post your full script here so that we have a chance to help you.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
- Bob Hansen
- Automation Wizard
- Posts: 2475
- Joined: Tue Sep 24, 2002 3:47 am
- Location: Salem, New Hampshire, US
- Contact:
I get the same results as Marcus. Here are four tests with the results:
Let>username=ADMIN
StringReplace>username,,,username
Watchlist: USERNAME=ADMIN
Let>username=ADMIN
StringReplace>username,,0,username
Watchlist: USERNAME=0ADMIN
Let>username=ADMIN
StringReplace>username,,1,username
Watchlist: USERNAME=1ADMIN
Let>username=ADMIN
StringReplace>username,,2,username
Watchlist: USERNAME=2ADMIN
---------------------------------
Also, you noted:
Let>username=ADMIN
StringReplace>username,,,username
Watchlist: USERNAME=ADMIN
Let>username=ADMIN
StringReplace>username,,0,username
Watchlist: USERNAME=0ADMIN
Let>username=ADMIN
StringReplace>username,,1,username
Watchlist: USERNAME=1ADMIN
Let>username=ADMIN
StringReplace>username,,2,username
Watchlist: USERNAME=2ADMIN
---------------------------------
Also, you noted:
The spaces are not showing up on the forum, try using "_" to disaplay their position. But I don't think the spaces will have any effect on the result.ADMIN
// note the spaces in the original
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!
Bob
A humble man and PROUD of it!
String Replace
Gents
Thank you for clearing the undergrowth in the woods and thanks for all of your help, I do appreciate it,
I don't understand the phrase from Marcus about setting the empty string null to zero though. From my understanding a variable can be a string or a numeric or a null is there an implied definition (System variable) that I will have triggered or can I somehow clear it to debug my code by for example resetting null to "null"?
Thank you for clearing the undergrowth in the woods and thanks for all of your help, I do appreciate it,
I don't understand the phrase from Marcus about setting the empty string null to zero though. From my understanding a variable can be a string or a numeric or a null is there an implied definition (System variable) that I will have triggered or can I somehow clear it to debug my code by for example resetting null to "null"?
- Marcus Tettmar
- Site Admin
- Posts: 7395
- Joined: Thu Sep 19, 2002 3:00 pm
- Location: Dorset, UK
- Contact:
By way of explanation, run this script:
It reproduces what you are seeing. Why? Look at that first line. I've set NOTHING to " 0".
Somewhere in your script that is what you have done. Not intentionally of course, but somewhere you have probably specified nothing in place of a return var.
To find out where, step through your script and look at the watch list. When you see "= 0" in the watch list. I.e. where you see a line that STARTS with an equals sign. Then you've found the line that is causing your problem.
As I said - if you want us to find the problem, send your script. Without your script we're playing guessing games.
Code: Select all
Let>= 0
Let>theString=<UserLogon>ADMIN</UserLogon>
StringReplace>theString,<UserLogon>,,theString
MessageModal>theString
Somewhere in your script that is what you have done. Not intentionally of course, but somewhere you have probably specified nothing in place of a return var.
To find out where, step through your script and look at the watch list. When you see "= 0" in the watch list. I.e. where you see a line that STARTS with an equals sign. Then you've found the line that is causing your problem.
As I said - if you want us to find the problem, send your script. Without your script we're playing guessing games.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?