How do I deal with special characters in vb.net?

12,494

Regular expressions have a different escape syntax and rules than VB.NET. Since you're dealing with a regex string in your code, you have to make sure the string is escaped properly for regex and VB.NET.

In your example, the - needs to be escaped with a ...

Regex.IsMatch(Result.Text, "^[`\-/]")

To match any character in the provided string, try this...

Regex.IsMatch(Result.Text, "[`~!@#\$%\^&\*\(\)_\-\+=\{\}\[\]\\\|:;""'<>,\.\?/]")
Share:
12,494
Ashtopher
Author by

Ashtopher

Updated on June 04, 2022

Comments

  • Ashtopher
    Ashtopher over 1 year

    special character set: `~!@#$%^&*()_-+={}[]\|:;""'<>,.?/

    Is this the right way to search for items within that special character set?

        Regex.IsMatch(Result.Text, "^[`-/]")
    

    I always get an error.... Do I need to use ASCII codes? If so, how can I do this? Thank you in advance!

  • Ashtopher
    Ashtopher over 9 years
    Thank you! I finally understand this now :D