Re: Regular Expressions..
I can show you, but first I'm going to not recommend it.
Regular expressions are a pretty big tool; when you use a regex you essentially ask the computer at runtime to fire up code that compiles the regex into something that does the parsing for you. You can mitigate this overhead, but the fact that it's there makes regex somewhat inappropriate for lightweight tasks.
This, in addition to the notion that a regex is generally harder to understand, makes regex a poor tool for very simple text replacement tasks. String.Replace() does a good job for simple replacements:
Code:
Dim myString As String = "asdf, jkl, qwerty"
myString = myString.Replace(",", "~")
If you insist, have a look at Regex.Replace(). You need three things to use Regex.Replace():
- The pattern that defines what should be replaced.
- The string that will have things replaced.
- The string that will replace the things that match the pattern.
In your case, you have a string where you want to replace "," with "~". Usage might look like this:
Code:
Dim myString As String = "asdf, jkl, qwerty"
Dim regex As New Text.RegularExpressions.Regex(",")
myString = regex.Replace(myString, "~")
Honestly, it's a little more work for such a simple substitution. I'd recommend reserving regex for more complicated replacement tasks, like "replace ',,' but leave ',,,' alone" or something crazy like "Foo value = new Foo();" to "Dim value As New Foo()".
Re: Regular Expressions..
Totally agree with Sitten and was exactly what I was going to recommend for the same reasons.