Using regular expressions to validate IP address with Delphi XE

Let's share the knowledge with your friends

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


Let's share the knowledge with your friends
21 replies
  1. ChAr
    ChAr says:

    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.

    Reply
  2. Michael Justin
    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”.

    Reply
  3. Atle
    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.

    Reply
    • Andreano Lanusse
      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 🙂

      Reply
  4. Stefan
    Stefan says:

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

    Reply
  5. john
    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!)

    Reply
  6. Andreano Lanusse
    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.

    Reply
  7. Stefan
    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 $. 😉

    Reply
  8. john
    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
      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.

      Reply
  9. Fabricio
    Fabricio says:

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

    Reply
  10. Carl
    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.

    Reply

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.