RegExp | Andreano Lanusse | Technology and Software Development Where Andreano Lanusse talk about technology, software development, programming techniques, databases, games and more through articles, tutorials and videos Wed, 02 Feb 2011 11:13:11 +0000 en hourly 1 https://wordpress.org/?v=6.7.4 Using regular expressions to validate IP address with Delphi XE http://www.andreanolanusse.com/en/using-regular-expressions-to-validate-ip-address-with-delphi-xe/ http://www.andreanolanusse.com/en/using-regular-expressions-to-validate-ip-address-with-delphi-xe/#comments Thu, 09 Sep 2010 06:57:30 +0000 http://www.andreanolanusse.com/en/?p=349 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 […]

Andreano Lanusse | Technology and Software Development Follow me on Twitter: @andreanolanusse

]]>
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

Andreano Lanusse | Technology and Software Development Follow me on Twitter: @andreanolanusse

]]>
http://www.andreanolanusse.com/en/using-regular-expressions-to-validate-ip-address-with-delphi-xe/feed/ 21