Using regular expressions to validate IP address with Delphi XE

Categories:DelphiTags: ,

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;



 ipRegExp : String;

    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')
    Writeln('Text DOES NOT match the regular expression');

    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);

Just execute the program and pass the IP address as parameter.

In this case the IP address is valid
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
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

21 Responses to Using regular expressions to validate IP address with Delphi XE

  1. Reply ChAr says:

    Hi, I think that in Your second example the “invalid” IP adress is actually perfectly valid one. 243 is NOT greater than maximum of 255.

  2. Reply pedrow says:

    Can you just check, is your second example right?

  3. Reply Jeroen Pluimers says:

    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).


  4. Reply Michael Justin says:

    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”.

  5. Reply Atle says:

    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.

  6. Reply Uwe Raabe says:

    @Atle: The output actually states that the IP address is correct!

  7. Reply Stefan says:

    Actually your example checks if the given text *contains* a valid IP adress, not if it *is* a valid IP adress.

    • Reply Andreano Lanusse says:

      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 ๐Ÿ™‚

  8. Reply Stefan says:

    Nope, you are using word boundaries instead of start and end of string anchors. “Hello World” would match the regex.

  9. Reply john says:

    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!)

  10. Reply Andreano Lanusse says:

    Interesting Stefan, so I had to change the regexp I used to not allow that ๐Ÿ™‚

  11. Reply Andreano Lanusse says:

    @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.

  12. Reply Stefan says:

    @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 $. ๐Ÿ˜‰

  13. Reply john says:

    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.

    • Reply Andreano Lanusse says:

      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.

  14. Reply Fabricio says:

    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.

  15. Reply Please Help me! says:

    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 :
    thank you.

  16. Reply Carl says:

    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.

Leave a Reply

Your email address will not be published. Required fields are marked *