[.net] [VB.NET] regular expressions

Started by
7 comments, last by Sirisian 17 years, 1 month ago
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.
Advertisement
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.
-----------------------------If pi is used to find the dimensions of a pie,Is cak used to find the dimensions of a cake?
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?
[size="2"]Don't talk about writing games, don't write design docs, don't spend your time on web boards. Sit in your house write 20 games when you complete them you will either want to do it the rest of your life or not * Andre Lamothe
"^(\-?[\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 :)
You can use regular expression ^[-+]?\d*$ to check for negative and positive integers.

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]

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.

[Website] [+++ Divide By Cucumber Error. Please Reinstall Universe And Reboot +++]

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 ? :(
Thank you guys so much. I almost forgot about the built in help thing.

I think I understand it now, the whole, ^ and ? messed me up before, but now I get it.

This is just for a small map editor, so I probably don't need to compensate for the international , instead of . thing.

This topic is closed to new replies.

Advertisement