开发者

Getting the whole word (link) from a phrase, when I know some of it

开发者 https://www.devze.com 2023-03-07 06:54 出处:网络
Lets say I have a String: Go to this page: http://mysite.com/?page开发者_Python百科=1 , and I have a string page. I would like to create a function like so:

Lets say I have a String: Go to this page: http://mysite.com/?page开发者_Python百科=1 , and I have a string page. I would like to create a function like so:

MyBoolean := IsLink('Go to this page: http://mysite.com/?page=1','page',sLink); 
// sLink is a Var, so it would return http://mysite.com/?page=1

Basically it is supposed to check if the word "page" is part of a link or not.

However I just can't figure it out. Any tips?


You could do something like

function GetLinkContaining(const Str, SubStr: string; out URL: string): boolean;
const
  ValidURLSpecialChars = ['.', ':', '/', '?', '=', '&', '%'];
  Prefixes: array[0..4] of string = ('http://', 'https://', 'ftp://', 'mailto:',
    'www.');

  function IsValidURLChar(const Char: char): boolean;
  begin
    result := IsLetterOrDigit(Char) or (Char in ValidURLSpecialChars);
  end;

var
  SubStrPos: integer;
  Start, &End: integer;
  i: Integer;
  URLBegin: integer;
begin
  result := false;

  URLBegin := 0;
  for i := low(Prefixes) to High(Prefixes) do
  begin
    URLBegin := Pos(Prefixes[i], Str);
    if URLBegin > 0 then
      break;
  end;
  if URLBegin = 0 then Exit(false);

  SubStrPos := PosEx(SubStr, Str, URLBegin);
  if SubStrPos = 0 then Exit(false);

  Start := SubStrPos;
  for i := SubStrPos - 1 downto 1 do
    if IsValidURLChar(Str[i]) then
      dec(Start)
    else
      break;
  &End := SubStrPos + length(SubStr);
  for i := SubStrPos + length(SubStr) to length(Str) do
    if IsValidURLChar(Str[i]) then
      inc(&End)
    else
      break;
  URL := Copy(Str, Start, &End - Start);
  result := true;
end;

To test it:

procedure TForm1.FormCreate(Sender: TObject);
var
  s: string;
begin
  if GetLinkContaining('Go to this page: http://mysite.com/?page=1 (right now!)',
    'page', s) then
    ShowMessage(s);
  if GetLinkContaining('This is my favourite site (www.bbc.co.uk).', 'bbc', s) then
    ShowMessage(s);        
end;


To check if 'page' is part of a string you can use the function Pos.

function Pos(Str, Source : string): integer;

Pos returns an integer specifying the position of the first occurrence of one string within another.

Pos looks for the first complete occurence of Str in Source. If it finds one, it returns the character position in Source of the first character in Str as an integer value, otherwise it returns 0. Pos is case sensitive. So mybe you have to deal with upper- and lowe-case situations.

To extracrt the URL is (maybe) not so easy, you have to define more conditions. If the URL is always at the end of your string, you can copy everything from the http on (also use Pos and Copy!)


Among the more powerful string matching algorithms there are regular expressions. They allow for very complex matches without writing much code, although mastering them may require a little time. Latest versions of Delphi have already regular expression libraries, but you can find some for earlier versions as well.

0

精彩评论

暂无评论...
验证码 换一张
取 消