Filter out unwanted characters in the string
Dim a, b As String a = "123 % Hello $ 456 world 789 $"
' b = "123 Hello 456 world 789" b = System.Text.RegularExpressions.Regex.Replace(a, "[^\w\s]", "").Trim b = System.Text.RegularExpressions.Regex.Replace(b, "\s+", " ")
' b = "12345678" b = System.Text.RegularExpressions.Regex.Replace(a, "[^\d]", "") b = System.Text.RegularExpressions.Regex.Replace(b, "\s+", " ")
' b = "Hello world" b = System.Text.RegularExpressions.Regex.Replace(a, "[^A-Za-z ]", "").Trim b = System.Text.RegularExpressions.Regex.Replace(b, "\s+", " ")
The melody of logic will always play out the truth. ~ Narumi Ayumu, Spiral
The expression \w includes all letters and numbers, the \s all spaces. The ^ negates the expression, so all invalid characters get replaced with an empty string.
The expression \d includes all decimal digit. The ^ negates the expression, so all invalid decimal digit get replaced with an empty string.
The expression \s+ includes one or more spaces, so regardless one or more spaces get replaced with a space