Sign in to follow this  
Sirisian

[.net] [VB.NET] regular expressions

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this