Regular Expression !

  • Thread starter Thread starter Marcel Hug
  • Start date Start date
M

Marcel Hug

Hi all !
I have a little question !
I would like to check if a Ip address matches the notation of IPv4.
I do this by regular expression with the following expression:

\b((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-
9][0-9]?)\b

It works fine for the most cases, but it is possible to insert a string
like:
192.168.23.20-0

How can i fit this failure ?

Thanks
Regards
Marcel
 
Marcel said:
Hi all !
I have a little question !
I would like to check if a Ip address matches the notation of IPv4.
I do this by regular expression with the following expression:

\b((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-
9][0-9]?)\b

It works fine for the most cases, but it is possible to insert a string
like:
192.168.23.20-0

How can i fit this failure ?

How about using System.Net.IPAddress.Parse ?
 
Marcel said:
Hi all !
I have a little question !
I would like to check if a Ip address matches the notation of IPv4.
I do this by regular expression with the following expression:

\b((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-
9][0-9]?)\b

It works fine for the most cases, but it is possible to insert a
string like:
192.168.23.20-0

How can i fit this failure ?

Thanks
Regards
Marcel

The \b matches a word boundary, such as between that "0" and "-".
"bla-192.168.23.20" would also match.

What you want is to check for *input* start and end:
start your RE with a "^" (instead of "\b") and end with "$"
(again instead of "\b").

Hans Kesting
 
\b((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-
9][0-9]?)\b

It works fine for the most cases, but it is possible to insert a string
like:
192.168.23.20-0

How can i fit this failure ?

The other posts assumed that you were matching a single IP. If that is
true you can use their solution. If however you are searching for all
IP adresses in a string you will need to do this.

Assuming RegexOptions.IgnorePatternWhitespace (Which is needed to make
regexs readable) and formatted using a fixed width font:

(?<! \S ) # Look behind assertion that previous character isn't a
non space character

(
(25[0-5] | 2[0-4][0-9] | [01]?[0-9][0-9]?)# A valid number
\. # Followed by a dot
){3} # Repeated three times

(25[0-5] | 2[0-4][0-9] | [01]?[0-9][0-9]?) # The last number

(?! \S ) # Look ahead assertion that next character isn't a non
space character

\b can only find alphanumeric boundries since it is a zero-width
assertion. I replaced those with assertions that look forward and
backward.

For more information look in documentation at the regex language
elements->Grouping constructs. For mor information, this webpage
http://www.regular-expressions.info/lookaround.html explains it in
more detail.
 
Marcel Hug said:
Hi all !
I have a little question !
I would like to check if a Ip address matches the notation of IPv4.
I do this by regular expression with the following expression:

\b((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-
9][0-9]?)\b

It works fine for the most cases, but it is possible to insert a string
like:
192.168.23.20-0

How can i fit this failure ?

As Larry suggested, use IPAddress.Parse, or if you're using .NET 2.0,
IPAddress.TryParse. If you need to parse multiple addresses, break the
string up into its constituent parts (either with String.Split, or with
Regex.Split if you want to be somewhat more flexible) and then parse
each of them.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top