[.net] [VB.NET] regular expressions
I've been looking at tutorials for about 2 hours. Isn't regular expressions used to figure out if input meets a certain criteria?
I tried to use system.Text.regularexpressions.regex to check if a user enters only integers in list box, but it's not working well.
I believe the regex string for numbers is "[0-9]" for numbers, but I need a hyphen for negatives. Also it would be nice to know how to add a ".", but I figure it's the same as adding a hyphen in the string.
Dim reg As New System.Text.RegularExpressions.Regex("[0-9]")
Does, reg.IsMatch("0123456789") just check if the parser string shows up anywhere in the string? I need to find out of anything other than the regex string shows up in the string.
I'd be grateful for any help or tutorials. The first two tutorials on google when I searched for "VB.NET regular expressions" didn't help me. I figure I'm using them wrong.
Use Visual Studio's built-in help. Searching the index for regular expressions gives some good results. I never knew you could do all this stuff with that class, actually. Thanks for bringing this up.
You can also try adding a regular expression validator and bringing up the regular expression editor and seeing how microsoft does some of the common ones like zip code-\d{5}(-\d{4})?
or phone number - ((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}
U.S. SSN - \d{3}-\d{2}-\d{4}
that might give you an idea?
or phone number - ((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}
U.S. SSN - \d{3}-\d{2}-\d{4}
that might give you an idea?
"^(\-?[\d]+)\.?[\d]*" should work.
Start of line, optional '-' char, at least one digit, optional '.' followed by 0 or more digits.
And don't forget, there are many way to do the same thing in regex :)
Start of line, optional '-' char, at least one digit, optional '.' followed by 0 or more digits.
And don't forget, there are many way to do the same thing in regex :)
You can use regular expression ^[-+]?\d*$ to check for negative and positive integers.
Here's some code:
Here's some code:
Public Function CheckIfValidIntegerString(ByVal inputString As String) As Boolean Dim regex As System.Text.RegularExpressions.Regex = New System.Text.RegularExpressions.Regex("^[-+]?\d*$", System.Text.RegularExpressions.RegexOptions.IgnoreCase Or System.Text.RegularExpressions.RegexOptions.CultureInvariant Or System.Text.RegularExpressions.RegexOptions.IgnorePatternWhitespace Or System.Text.RegularExpressions.RegexOptions.Compiled) If (regex.IsMatch(inputString) = False) Then Return False End If Return True End Function
Wouldn't "^[-+]?\d*$" allow for just + or - on it's own with no digits and not handle a decimal point ? whereas "^(\-?[\d]+)\.?[\d]*" which I already posted would, albeit I didn't include the end of line check, so "^(\-?[\d]+)\.?[\d]*$"
I wouldn't use that regular expression to validate numeric input, as it will break on non-English locales. For example, a Frenchman might want to enter one and a half ("1,5"). [smile]
CheckIfValidIntegerString is a bit of a misnomer as it'll handle non-integral types. You should get the general idea, though!
Double.TryParse on MSDN.
Public Function CheckIfValidIntegerString(ByVal inputString As String) As Boolean Dim d As Double Return Double.TryParse(inputString, d)End Function
CheckIfValidIntegerString is a bit of a misnomer as it'll handle non-integral types. You should get the general idea, though!
Double.TryParse on MSDN.
Doh, locales yeah :D already been stung on that one by having word wrapping but the french (and maybe others) have a space between word and ? :(
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement