【问题标题】:Delphi Regular Expressions IMDBDelphi 正则表达式 IMDB
【发布时间】:2014-08-25 19:36:55
【问题描述】:

我正在尝试将此 c# 类移植到 Delphi: http://lab.abhinayrathore.com/imdb/imdb_asp_csharp.htm

我已经编写了 Helper 类,例如 MAtchAll 等,并编写了从网页获取 http 的代码。

但我的问题是这一行:

ArrayList imdbUrls = matchAll(@"<a href=""(http://www.imdb.com/title/tt\d{7}/)"".*?>.*?</a>", html);

这是我的 MatchAll 版本

function MatchAll(const aExpression, aHtml: string; i: Integer = 0): TStringDynArray;
var
  ResultArray: TStringDynArray;
  Match: TMatch;
begin
  SetLength(ResultArray, 0);
  for Match in TRegEx.Matches(aHtml, aExpression, [roMultiLine]) do
  begin
    SetLength(ResultArray, length(ResultArray) + 1);
    ResultArray[length(ResultArray) - 1] := Match.Groups[i].Value.Trim;
  end;

  Result := ResultArray;
end;

还有我的 GetUrlData 版本

function RandomNext(const AFrom, ATo: Integer): string;
begin
  Result := IntToStr(Random(ATo - AFrom) + AFrom);
end;

function GetUrlData(const Url: string): string;
var
  Client: TIdHTTP;
begin
  Client := TIdHTTP.Create(nil);
  // Random IP Address
  Client.Request.CustomHeaders.AddValue('X-Forwarded-For', Format('%d.%d.%d.%d', [Random(255), Random(255), Random(255), Random(255)]));

  // Random User-Agent
  Client.Request.CustomHeaders.AddValue('User-Agent', 'Mozilla/' + RandomNext(3, 5) + '.0 (Windows NT ' + RandomNext(3, 5) + '.' + RandomNext(0, 2) + '; rv:2.0.1) Gecko/20100101 Firefox/' +
    RandomNext(3, 5) + '.' + RandomNext(0, 5) + '.' + RandomNext(0, 5));

  Result := Client.Get(Url);
  FreeAndNil(Client);
end;

鉴于此网址:http://www.google.com/search?q=imdb+13%20sins

如何提取 IMDB 网址?

延斯·博瑞肖特

【问题讨论】:

  • 转义这部分的点,如http://www\.imdb\.com/title
  • 感谢您的回答 这成功了:www\.imdb\.com/title/tt\d{7}
  • 如果我是你,我会使用the IMDB API

标签: regex delphi imdb


【解决方案1】:

这成功了:www\.imdb\.com/title/tt\d{7}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-06
    • 2015-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多