How to do regular expressions in vb.net

alsrory

New Member
So there are many questions and answers here around the subject of regular expressions. The downside is that the vast majority of answers are simply the regular expression...I have also googled - and found hundreds of sites. Trying to wade through everything for a quick-to-understand and implement answer isn't too easy. they are either in a different language - which maybe shouldn't make any difference, though you escape differently in C# to VB and that leads to confusion as to what is an escape character vs a regex switch.The part I am struggling with is understanding them so I can implement some, apparently, simple expressions.My scenario:I have to check every character in a given string, and if the regular expression doesn't allow any of the characters then it should return false.Example:I have tried the following expressions (copy/pasted from various answers here....)\[code\]Dim r As New Regex("^[a-zA-Z0-9 ]*$")\[/code\]also tried\[code\]Dim r As New Regex("[a-zA-Z0-9\s]")\[/code\]also tried\[code\]Dim r as New Regex("^[A-Za-z0-9 ]*")\[/code\]I have been implementing this like:\[code\]Dim r As New Regex(_fontJIAdieuxRegEx) '' where _fontJIAdieuxRegEx is one of the above regex strings.Dim supported = r.IsMatch(fontName)\[/code\]I have been trying to validate something like the following:\[code\]darren\[/code\] should return \[code\]True\[/code\]\[code\]da-rren\[/code\] should return \[code\]False\[/code\] due to the \[code\]-\[/code\] hyphen\[code\]da rren\[/code\] should return \[code\]True\[/code\]Now, simply put, any of these expressions will either return \[code\]True\[/code\] for all of the strings or \[code\]False\[/code\] for all of the strings; so i am clearly doing something wrong.what I would really appreciate is someone pointing out where I am going wrong and also explain a little about the make-up of the regular expression.Once I understand them a little more I need to be able to have different expressions to allow other characters, such as ! @ " ' . etc. So please don't just paste an expression to solve the simple example above.
 
Back
Top