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;

{$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

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

    –jeroen

  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 127.0.0.1 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:

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

  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 : Alexander.Alkhine@gmail.com
    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 *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>