如何提取域名后缀?

How to extract domain suffix?

如何在不输入 http://https:// 的情况下提取域后缀? 比如我输入whosebug.com,我想得到com的结果。

我有这个功能,但是我必须输入http://才能得到结果

有什么方法可以跳过输入 http://https:// 吗?

procedure TForm1.Button2Click(Sender: TObject);
  function RatChar(S:String; C: Char):Integer;
  var
    i : Integer;
  begin
    i := Length(S);
    //while (S[i] <> C) and (i > 0) do
    while (i > 0) and (S[i] <> C) do
      Dec(i);
    Result := i;
  end;
var
  uri: TIdURI;
  i: Integer;
begin
  uri := TidURI.Create(Edit2.Text);
  try
    //Memo1.Lines.Add(uri.Protocol);
    //Memo1.Lines.Add(uri.Host);
    i := RatChar(uri.Host, '.');
    Memo1.Lines.Add(Copy(uri.Host, i+1, Length(uri.Host)));
    Memo1.Lines.Add(uri.Document);
  finally
    uri.Free;
  end;
end;

根据建议 Extracting top-level and second-level domain from a URL using regex 它应该 运行 在 Delphi 中像这样:

program Project1;
{$APPTYPE CONSOLE}
{$R *.res}
uses
  System.SysUtils, System.RegularExpressions;
var
  url,
  rePattern: string;
  rMatch   : TMatch;
  rGroup   : TGroup;
  arr      : TArray<string>;
begin
  try
    url := 'https://www.answers.com/article/1194427/8-habits-of-extraordinarily likeable-people';
    //url := '
    rePattern := '^(?:https?:\/\/)(?:w{3}\.)?.*?([^.\r\n\/]+\.)([^.\r\n\/]+\.[^.\r\n\/]{2,6}(?:\.[^.\r\n\/]{2,6})?).*$';
    rMatch := TRegEx.Match(url, rePattern);
    if rMatch.Success then
    begin
      rGroup := rMatch.Groups.Item[pred(rMatch.Groups.Count)];
      arr := rGroup.Value.Split(['.']);
      writeln('Top-Level-Domain: ', arr[High(arr)]);
    end
    else
     writeln('Sorry');
    readln;
  except
   on E: Exception do
     Writeln(E.ClassName, ': ', E.Message);
  end;
end.

但是,此正则表达式仅在提供 www. 时有效。

uses
  System.SysUtils;
var
  u  : string;
  arr: TArray<string>;
begin
  try
   u   := '
   arr := u.Split(['://'], TStringSplitOptions.ExcludeEmpty);
   u   := arr[High(arr)]; //whosebug.com/questions/71166883/how-to-extract-domain-suffix';
   arr := u.Split(['/'], TStringSplitOptions.ExcludeEmpty);
   u   := arr[0]; //whosebug.com
   arr := u.Split(['.'], TStringSplitOptions.ExcludeEmpty);
   u   := arr[High(arr)]; //com
   writeln('Top-Level-Domain: ', u);
   readln;
 except
   on E: Exception do
     Writeln(E.ClassName, ': ', E.Message);
 end;