Connecting Tech Pros Worldwide Forums | Help | Site Map

Need help with regex

Michael R. Pierotti
Guest
 
Posts: n/a
#1: Nov 21 '05
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



Jay B. Harlow [MVP - Outlook]
Guest
 
Posts: n/a
#2: Nov 21 '05

re: Need help with regex


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" <mrp@hafatel.com> wrote in message
news:uUY2Ia45EHA.3120@TK2MSFTNGP12.phx.gbl...[color=blue]
> 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
>
>[/color]


Michael R. Pierotti
Guest
 
Posts: n/a
#3: Nov 21 '05

re: Need help with regex


Thank mate how did I miss that ? LOL

"Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP@msn.com> wrote in message
news:uYSJaT95EHA.2512@TK2MSFTNGP09.phx.gbl...[color=blue]
> Michael,
> "." matches any character except \n. To match a period you need to[/color]
"escape"[color=blue]
> 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" <mrp@hafatel.com> wrote in message
> news:uUY2Ia45EHA.3120@TK2MSFTNGP12.phx.gbl...[color=green]
> > 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[/color][/color]
only[color=blue][color=green]
> > 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
> >
> >[/color]
>
>[/color]


Closed Thread


Similar Visual Basic .NET bytes