Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

StringBuilder.Replace and Wildcards 1

Status
Not open for further replies.

jmeckley

Programmer
Jul 15, 2002
5,269
0
0
US
I have a string builder that contains place holders. Here is an example of 3 place holders in the file:
[Name]
[Phone]
[Features]

Once I replace all my values I want to cycle back through the string and remove all the place holders that were not replaced previously.

I tried Msg.Replace("[?]", "") and Msg.Replace("[*]", ""), but they do not work. Is it possible to use wild cards with the replace function?

Jason Meckley
Database Analyst
WITF
 
Jason,
In your case, I would use the Replace method of the Regex class as opposed to the Replace method of StringBuilder.

Try this:
Code:
Regex re = [COLOR=blue]new[/color] [b]Regex(@"\[\w+]");[/b]
[COLOR=green]sb is your StringBuilder object[/color]
[COLOR=blue]string[/color] s = re.Replace(sb.ToString(), "[]");
If you're new to Regualar Expressions, the above might seem a little strange but let me know and I'll explain step by step what it means. You could also read more about Regular Expressions at the MSDN site.

Hope this helps!

JC


Friends are angels who lift us to our feet when our wings have trouble remembering how to fly...
 
Oops,
About the following line:
Code:
string s = re.Replace(sb.ToString(), [b]"[]"[/b]);
You should change "[]" and make it whatever string it is that you want to replace your placeholders with. I didn't notice (until now) that you want to replace your placeholders with an empty string. That being the case, the line above should be...
Code:
string s = re.Replace(sb.ToString(), "");
JC

Friends are angels who lift us to our feet when our wings have trouble remembering how to fly...
 
thanks for the code. I did some digging and found Regex in the system.text.regularexpressions name space.

I have a very basic understanding of regular expressions.
I know you have to start with a \ the [ and ] characters are literal. I'm not sure about \w+ though.

I'm using VB and didn't need the @ character. It gave me an error since it wasn't inside the quotes too.

thanks again for the post!

Jason Meckley
Database Analyst
WITF
 
My apologies for not mentioning that Regex was in the System.Text.RegularExpressions namespace.

This is what \[\w+] means:

\[ - Regualar Expressions don't have to start with a backslash (\). The reason why we started with a \ in this case, is that the opening square bracket has a special meaning to the RegEx parser and thus you have to escape it using the backslash. \[ means that "start with an opening backslash"

\w - This means a word character which is an alphanumeric character or the underscore character.

+ - The plus sign means "find one or more". Thus \w+ means find one or more alphanmeric characters. The * character, by the way, means "find zero ore more".

] - The closing square bracket doesn't need to be escaped, thus we don't need a \ before it.

Thus, the whole expression, \[\w+], means "match one or more alphanumeric characters that are surrounded by squre brackets". If you want this to say "match zero or more..." then you have to change your regular expression to \[\w*].

Hopes this helps!

JC

Friends are angels who lift us to our feet when our wings have trouble remembering how to fly...
 
thanks. this will help me with future regexs as well.

Jason Meckley
Database Analyst
WITF
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top