Rozpoznanie hipertekstu

0

Cześć, próbuję rozkminić, jak rozpoznać hipertekst w stringu, ale nie wychodzi mi to. Może Wy możecie mnie jakoś na to naprowadzić?
(ja wiem, że copy + pos + f1 ;))

Robię klasę, która ma to rozpoznawać. Mam tablicę stringów, które stanowią wzorce hipertekstu, np:

'http://'+WORD;
'www.'+WORD+'.'+WORD

Gdzie WORD to jakaś stała(np: --##WORD#-?&) określająca, że w tym miejscu musi być dowolne słowo(słowo, czyli minimum 2 znaki)

I teraz jak rozpoznać, że w przykładowym łańcuchu:

s:='Oto jest przykładowy łańcuch zawierający hipertekst: www.wp.pl , a ten jest już nieprawidłowy: www.wp.';

jest hipertekst: www.wp.pl?

Na razie potrzebuję tylko wiedzieć, że hipertekst się znajduje.

Macie jakieś wskazówki?

0

poszukaj implementacji wyrażeń regularnych dla Delphi i naucz się nich

0

Nie wiem jak wyglądają regexy w Delphi, ale masz gotowe wyrażenie:

http://[\w+\./\?=]+

Działa mi w pythonie. Możesz je rozszeżyć o obsługę innych znaków (w przykładzie poprawne są litery kropka, slash, znak zapytania oraz znak dolara)

Powodzenia.

0

Rozwiązanie z www.RegExpStudio.com:

{$B-}
unit HyperLinksDecorator;



interface

uses
 RegExpr;

type
TDecorateURLsFlags = (
 // describes, which parts of hyper-link must be included
 // into VISIBLE part of the link:
  durlProto, // Protocol (like 'ftp://' or 'http://')
  durlAddr,  // TCP address or domain name (like 'RegExpStudio.com')
  durlPort,  // Port number if specified (like ':8080')
  durlPath,  // Path to document (like 'index.html')
  durlBMark, // Book mark (like '#mark')
  durlParam  // URL params (like '?ID=2&User=13')
 );

TDecorateURLsFlagSet = set of TDecorateURLsFlags;


function DecorateURLs (
 // can find hyper links like 'http://...' or 'ftp://..'
 // as well as links without protocol, but start with 'www.'

 const AText : string;
 // Input text to find hyper-links

  AFlags : TDecorateURLsFlagSet = [durlAddr, durlPath, durlParam, durlBMark, durlPort, durlProto]
 // Which part of hyper-links found must be included into visible
 // part of URL, for example if [durlAddr] then hyper link
 // 'www.RegExpStudio.com/contacts.html' will be decorated as
 // '<a href="http://www.RegExpStudio.com/contacts.html">www.RegExpStudio.com</a>'

  ) : string;
 // Returns input text with decorated hyper links


function DecorateEMails (
 // Replaces all syntax correct e-mails
 // with '<a href="mailto:ADDR">ADDR</a>'
 // For example, replaces '[email protected]'
 // with '<a href="mailto:[email protected]">[email protected]</a>'.

 const AText : string
 // Input text to find e-mails

  ) : string;
 // Returns input text with decorated e-mails


implementation

uses
 SysUtils; // we are using AnsiCompareText

function DecorateURLs (const AText : string;
  AFlags : TDecorateURLsFlagSet = [durlAddr, durlPath, durlParam, durlBMark, durlPort, durlProto]
  ) : string; 
const 
  URLTemplate = 
   '(?i)' 
   + '(' 
   + '(FTP|HTTP)://'             // Protocol 
   + '|www\.)'                   // trick to catch links without
                                 // protocol - by detecting of starting 'www.'
   + '([\w\d\-]+(\.[\w\d\-]+)+)' // TCP addr or domain name
   + '(:\d\d?\d?\d?\d?)?'        // port number
   + '(((/[%+\w\d\-\\\.]*)+)*)'  // unix path
   + '(\?[^\s=&]+=[^\s=&]+(&[^\s=&]+=[^\s=&]+)*)?'
                                 // request (GET) params
   + '(#[\w\d\-%+]+)?';          // bookmark
var
  PrevPos : integer;
  s, Proto, Addr, HRef : string;
begin
  Result := ''; 
  PrevPos := 1; 
  with TRegExpr.Create do try 
     Expression := URLTemplate; 
     if Exec (AText) then 
      REPEAT 
        s := ''; 
        if AnsiCompareText (Match [1], 'www.') = 0 then begin
           Proto := 'http://';
           Addr := Match [1] + Match [3];
           HRef := Proto + Match [0];
          end
         else begin
           Proto := Match [1];
           Addr := Match [3];
           HRef := Match [0];
          end;
        if durlProto in AFlags
         then s := s + Proto;
        if durlAddr in AFlags
         then s := s + Addr;
        if durlPort in AFlags
         then s := s + Match [5];
        if durlPath in AFlags
         then s := s + Match [6];
        if durlParam in AFlags
         then s := s + Match [9];
        if durlBMark in AFlags
         then s := s + Match [11];
        Result := Result + System.Copy (AText, PrevPos,
         MatchPos [0] - PrevPos) + '<a target="_new" href="' + HRef + '">' + s + '</a>'; //###0.101
        PrevPos := MatchPos [0] + MatchLen [0];
      UNTIL not ExecNext;
     Result := Result + System.Copy (AText, PrevPos, MaxInt); // Tail
    finally Free;
   end;
end; { of function DecorateURLs
--------------------------------------------------------------}

function DecorateEMails (const AText : string) : string;
 const
  MailTemplate =
   '[_a-zA-Z\d\-\.]+@[_a-zA-Z\d\-]+(\.[_a-zA-Z\d\-]+)+';
 var
  PrevPos : integer;
 begin
  Result := '';
  PrevPos := 1;
  with TRegExpr.Create do try
     Expression := MailTemplate;
     if Exec (AText) then
      REPEAT
        Result := Result + System.Copy (AText, PrevPos,
         MatchPos [0] - PrevPos) + '<a href="mailto:' + Match [0] + '">' + Match [0] + '</a>';
        PrevPos := MatchPos [0] + MatchLen [0];
      UNTIL not ExecNext;
     Result := Result + System.Copy (AText, PrevPos, MaxInt); // Tail
    finally Free;
   end;
 end; { of function DecorateEMails
--------------------------------------------------------------}


end.

Kod zapisujemy w folderze programu pod nazwą hyperlinksdecorator.pas a następnie w naszym programie dajemy w USES: hyperlinksdecorator.
Wywołanie:
string:=decorateurls(string);
string:=decorateemails(string);

0
wala napisał(a)

Rozwiązanie z www.RegExpStudio.com:

Nie o to mi chodzi. Bo on zamienia. A ja ostatecznie chcę uzyskać listę hiperlinków.
Jutro popatrzę na te wyrażenia regularne. Swoją drogą nie wiedziałem, że ktoś to zaimplementował w Delphi

1 użytkowników online, w tym zalogowanych: 0, gości: 1