Using regular expressions to validate IP address with Delphi XE
One of the new Delphi XE features is the RTL support for regular expressions (unit RegularExpressions), regular expressions provide a concise and flexible means for matching strings of text, such as particular characters, words, or patterns of characters.
The follow example shows how to use regexp to validate IP address.
program RegExpIP; {$APPTYPE CONSOLE} uses SysUtils, RegularExpressions; var ipRegExp : String; begin try ipRegExp := '\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b'; if TRegEx.IsMatch(paramstr(1), ipRegExp) then Writeln('Text DOES match the regular expression') else Writeln('Text DOES NOT match the regular expression'); except on E: Exception do Writeln(E.ClassName, ': ', E.Message); end; end.
Just execute the program and pass the IP address as parameter.
In this case the IP address is valid
RegExpIP 200.100.2.21
Text DOES match the regular expression
In this case not, the IP address finish with 263, it is out of the range which is 255
RegExpIP 200.100.2.263
Text DOES NOT match the regular expression
On the RAD Studio demo repository at sourceforge you can find a project sample showing other regular expressions that you can use.
As well download the Delphi XE trial and start looking the other great features on this release, download the trial here
Hi, I think that in Your second example the “invalid” IP adress 200.100.2.243 is actually perfectly valid one. 243 is NOT greater than maximum of 255.
Can you just check, is your second example right?
This is in fact a much better example than the eternal email address validation (which – when you read the RFC’s closely – can only be approximated by regular expressions).
–jeroen
Hint: a simple number like 123456789 is also a valid IP address (if it is in the IPv4 or IPv6 address range). The dot-decimal notation is only the usual representation. Try it with “ping 123456789”.
This is wrong:
“In this case not, the IP address finish with 243, it is out of the range which is 255”..
243 is not outside the range.
@Atle: The output actually states that the IP address is correct!
All sorry, it was a typo from my side, the second example finish with 263 and the output message said DOES NOT match, so the program is correct, again my typo 🙂
Actually your example checks if the given text *contains* a valid IP adress, not if it *is* a valid IP adress.
Hi Stefan, it will check if the IP address is valid, as well it will restrict the 4 numbers in the IP address to 0..255.
But if you are considering if the IP address is valid on the network, not I’m not doing that, you can use the IP works ping component to check that 🙂
Nope, you are using word boundaries instead of start and end of string anchors. “Hello 127.0.0.1 World” would match the regex.
This post is a good example of why you don’t want regexps in your code: not only it is messy to look at, but it is also obviously hard to figure out if the regexp is correct or not (see the various comments above!)
Interesting Stefan, so I had to change the regexp I used to not allow that 🙂
@John, it’s not friendly the regexp mask, but it doesn’t mean it is not useful.
This is just a example how to use and it is very useful, I could create a separated function and make the code clear putting the regexp mask in other function.
@John: RegExps are very powerful, you have to learn how to use them if you are not familar with them. Prolly a case for solid unit testing.
@Andreano: Yes, simply change the \b at the beginning to ^ and the \b at the end to $. 😉
Well goto is powerful too, and it can allow to do more with less code, doesn’t mean you want to use it.
Andreano, compare your regexp with normal Delphi code one would use to do the validation: the code will be simpler, clearer, more maintainable and likely faster.
I understand John, but you can assume all of your strings will come from Delphi Applications, for example if you have a REST Server and need to get the content of HTML Input, you will need to parse the content which I think you will have more work then only use a RegExp.
I will blog more about that and will show how to write a clear code using RegExp.
@John
Andreano, compare your regexp with normal Delphi code one would use to do the validation: the code will be simpler, clearer, more maintainable and likely faster.
I believe it can clearer and more *readable*. Simpler I have some doubt. And I’m pretty sure that, for getting information on large strings, nothing can be faster than regex… (Unless Emb implementation had done something much wrong).
Regex is (maybe just one of them) the little secret that makes PERL almost invincible in text processing.
Help Me ,My Dear Friend!
I Want Create A program With Delphi7,that Ping Many Ips in one Time(Real Time).
please Send for me your Standard Delphi7 request in : Alexander.Alkhine@gmail.com
thank you.
Alexander, the ipWorks components included in Delphi XE has many internet components, one of them is the Ping component, it does what are you looking for.
How can I use this component for C++ Builder XE? My friend Brent and I are trying to use regex’s to parse content from Web Responses.
Carl,
You can use RegExp in C++ as I describe in this blog post http://www.andreanolanusse.com/en/using-regular-expression-on-cbuilder-firemonkey-application/