# [.net] [VB.NET] regular expressions

This topic is 4007 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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.

##### Share on other sites
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.

##### Share on other sites
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?

##### Share on other sites
"^(\-?[\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 :)

##### Share on other sites
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

##### Share on other sites
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]*$"

##### Share on other sites
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.

##### Share on other sites
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 ? :(

##### Share on other sites
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.