{n]UUID[/n}{v]rec4[/v}{n]ModTS[/n}{v][/v}{n]ModUser[/n}{v][/v}{n]NameFirst[/n}{v]Pepper[/v}{n]NameLast[/n}{v]Potts![/v}{n]NameCompany[/n}{v]Stark Industries[/v}{n]Notes[/n}{v]Assistant to Tony[/v}
I’d like to create two strings from this using Kem’s awesome m_string module. Field names are wrapped in “{n][/n}” for field names and “{v][/v}” for field values.
Trying to get all the names by removing all the values.
[code] dim sourceStr as String = “{n]UUID[/n}{v]rec4[/v}{n]ModTS[/n}{v][/v}{n]ModUser[/n}{v][/v}{n]NameFirst[/n}{v]Pepper[/v}{n]NameLast[/n}{v]Potts![/v}{n]NameCompany[/n}{v]Stark Industries[/v}{n]Notes[/n}{v]Assistant to Tony[/v}”
dim modifyStr as String = “” + sourceStr.ReplaceAll("{n]", “”).ReplaceAll("[/n}{v]", “”).ReplaceAll("[/v}", “”) + “”
[/code]
The problem is greediness. In this case you have to turn it off.
Think of it this way. Let’s say you want all the text between the “@” symbol, so you might write a pattern like this: @.*@. All well and good if your string is “@field@”, but what if it’s “@field1@ some text @field2@”? What you’ll match by default is “@field1@ some text @field2@” instead of just “@field1”. Why? Because the pattern was told to be “greedy”, that is, match as much as it can.
Looking at it another way, you are saying, “start looking for the first @ from the start of the string, then look for the last @ from the back of the string.”
By turning off Greedy, you tell it to stop as soon as it has a complete match. You can do that by either filling in the greedy parameter on my ReplaceRegEx method, or by prefixing your pattern with code[/code]. The latter is more portable.
(The “U” stands for “Ungreedy”, which is an odd way to do it, but it is what it is.)
Thanks for that Norm. I did something similar effectively looping til all the strings I needed to remove were gone… The RegEx code is so much tighter though…