Delphi XE2 正则式判断IP

废话不说,上代码,简单之极。注意红色代码就可以了。

 

unit Unit4;

interface

uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, RegularExpressions;

type
TForm4 = class(TForm)
edtIP: TEdit;
btn1: TButton;
lbl1: TLabel;
procedure btn1Click(Sender: TObject);
private
{ Private declarations }
function CheckIPAddress(_IP: String): Boolean;
public
{ Public declarations }
end;

var
Form4: TForm4;

implementation

{$R *.dfm}

procedure TForm4.btn1Click(Sender: TObject);
begin
if CheckIPAddress(edtIP.Text) then
lbl1.Caption := 'The IP is valid!'
else
lbl1.Caption := 'The IP is invalid!';
end;

function TForm4.CheckIPAddress(_IP: String): Boolean;
begin
Result := TRegEx.IsMatch(_IP, '^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$');
end;

end.

posted @ 2012-05-19 02:17  极度生存  阅读(393)  评论(0编辑  收藏  举报