473,378 Members | 1,138 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Regular Expression !

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
Nov 17 '05 #1
4 1379

Marcel Hug wrote:
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 ?

--
Larry Lard
Replies to group please

Nov 17 '05 #2
Marcel Hug wrote:
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
Nov 17 '05 #3
On Thu, 6 Oct 2005 10:47:21 +0200, "Marcel Hug"
<ma******************@ATch.abb.com> wrote:
\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.

--
Marcus Andrén
Nov 17 '05 #4
Marcel Hug <ma******************@ATch.abb.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make...
4
by: Buddy | last post by:
Can someone please show me how to create a regular expression to do the following My text is set to MyColumn{1, 100} Test I want a regular expression that sets the text to the following...
4
by: Neri | last post by:
Some document processing program I write has to deal with documents that have headers and footers that are unnecessary for the main processing part. Therefore, I'm using a regular expression to go...
7
by: Billa | last post by:
Hi, I am replaceing a big string using different regular expressions (see some example at the end of the message). The problem is whenever I apply a "replace" it makes a new copy of string and I...
25
by: Mike | last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in matches. I would like to get what the actual regular expression is. In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.