467,901 Members | 1,763 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,901 developers. It's quick & easy.

Need help with regex

Dim reg As New Regex("^\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}$")
Dim m As Match = reg.Match(txtIPAddress.Text)
If m.Success Then
'No need to do anything here
Else
MessageBox.Show("You need to enter a valid IP Address", "Error!",
MessageBoxButtons.OK, MessageBoxIcon.Hand)
txtIPAddress.Focus()
Return
End If


I need to match a IP addy on this and I would think this should match only a
x.x.x.x to xxx.xxx.xxx.xxx pattern yet it passes as a match when I input
192168121113

Any ideas what I am doing wrong here ?

Thanks,

Mike
Nov 21 '05 #1
  • viewed: 1669
Share:
2 Replies
Michael,
"." matches any character except \n. To match a period you need to "escape"
it with a slash. "\." will match a period.

Try the following:

Const pattern As String = "^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$"
Static reg As New Regex(pattern, RegexOptions.Compiled)
Dim m As Match = reg.Match(txtIPAddress.Text)
If m.Success Then
'No need to do anything here
Else
MessageBox.Show("You need to enter a valid IP Address",
"Error!", MessageBoxButtons.OK, MessageBoxIcon.Hand)
End If

Unless I need the Match object specifically, I normally use Regex.IsMatch
instead.

Something like:
Const pattern As String = "^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$"
Static reg As New Regex(pattern, RegexOptions.Compiled)
If reg.IsMatch(txtIPAddress.Text) Then
'No need to do anything here
Else
MessageBox.Show("You need to enter a valid IP Address",
"Error!", MessageBoxButtons.OK, MessageBoxIcon.Hand)
End If

Hope this helps
Jay
"Michael R. Pierotti" <mr*@hafatel.com> wrote in message
news:uU**************@TK2MSFTNGP12.phx.gbl...
Dim reg As New Regex("^\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}$")
Dim m As Match = reg.Match(txtIPAddress.Text)
If m.Success Then
'No need to do anything here
Else
MessageBox.Show("You need to enter a valid IP Address", "Error!",
MessageBoxButtons.OK, MessageBoxIcon.Hand)
txtIPAddress.Focus()
Return
End If


I need to match a IP addy on this and I would think this should match only
a
x.x.x.x to xxx.xxx.xxx.xxx pattern yet it passes as a match when I input
192168121113

Any ideas what I am doing wrong here ?

Thanks,

Mike

Nov 21 '05 #2
Thank mate how did I miss that ? LOL

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:uY**************@TK2MSFTNGP09.phx.gbl...
Michael,
"." matches any character except \n. To match a period you need to "escape" it with a slash. "\." will match a period.

Try the following:

Const pattern As String = "^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$"
Static reg As New Regex(pattern, RegexOptions.Compiled)
Dim m As Match = reg.Match(txtIPAddress.Text)
If m.Success Then
'No need to do anything here
Else
MessageBox.Show("You need to enter a valid IP Address",
"Error!", MessageBoxButtons.OK, MessageBoxIcon.Hand)
End If

Unless I need the Match object specifically, I normally use Regex.IsMatch
instead.

Something like:
Const pattern As String = "^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$"
Static reg As New Regex(pattern, RegexOptions.Compiled)
If reg.IsMatch(txtIPAddress.Text) Then
'No need to do anything here
Else
MessageBox.Show("You need to enter a valid IP Address",
"Error!", MessageBoxButtons.OK, MessageBoxIcon.Hand)
End If

Hope this helps
Jay
"Michael R. Pierotti" <mr*@hafatel.com> wrote in message
news:uU**************@TK2MSFTNGP12.phx.gbl...
Dim reg As New Regex("^\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}$")
Dim m As Match = reg.Match(txtIPAddress.Text)
If m.Success Then
'No need to do anything here
Else
MessageBox.Show("You need to enter a valid IP Address", "Error!",
MessageBoxButtons.OK, MessageBoxIcon.Hand)
txtIPAddress.Focus()
Return
End If


I need to match a IP addy on this and I would think this should match only a
x.x.x.x to xxx.xxx.xxx.xxx pattern yet it passes as a match when I input
192168121113

Any ideas what I am doing wrong here ?

Thanks,

Mike


Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by hillcountry74 | last post: by
9 posts views Thread by jmchadha | last post: by
7 posts views Thread by Extremest | last post: by
15 posts views Thread by morleyc | last post: by
3 posts views Thread by =?Utf-8?B?UmF5IE1pdGNoZWxs?= | last post: by
8 posts views Thread by Alexander Vasilevsky | last post: by
4 posts views Thread by Danny Ni | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.