Delphi-PRAXiS
Seite 1 von 2  1 2      

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Programmieren allgemein (https://www.delphipraxis.net/40-programmieren-allgemein/)
-   -   Regex: alle html links eines String bekommen? (https://www.delphipraxis.net/129392-regex-alle-html-links-eines-string-bekommen.html)

Pseudemys Nelsoni 18. Feb 2009 10:19


Regex: alle html links eines String bekommen?
 
Hallo,

wie kann ich alle links eines HTML Codes bekommen? WIe muss mein Regexpattern dazu aussehen?

Der HTML Code sieht so aus:

Zitat:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Unbenanntes Dokument</title>
</head>

<body>


Bla</p>


Link1</p>


blubb</p>


Link2</p>


Link3 </p>


lala</p>
</body>
</html>

Nun muss ich ja sicher gehen, dass es auch wirklich ein link ist, dazu muss im pattern ja auch "<a href=" vorkommen und dann muss ich danach den Link der in " " steht bekommen.


WIe stell ich das an?

Meflin 18. Feb 2009 10:23

Re: Regex: alle html links eines String bekommen?
 
z.B. so:
Code:
[url="http://[^"]([^<]+)[/url]

alcaeus 18. Feb 2009 10:31

Re: Regex: alle html links eines String bekommen?
 
@Meflin:

Code:
[url="http://foo.bar"][img]xzy.png[/img][/url]
Q.E.D.

Bau rein dass kein "</a>" vorkommen darf, alles andere ist aua.

Greetz
alcaeus

Pseudemys Nelsoni 18. Feb 2009 10:34

Re: Regex: alle html links eines String bekommen?
 
Danke erstmal euch zwei.

Bei dem Code von Meflin bekomme ich keine Matches :\.

Ich würd den pattern ja auch selber umändern, aber Regexe versteh ich wirklich nicht so...


alcaeus, was meinst du damit? bzw wie stell ich das an? ich weiß bloß, dass mit ^ negiert wird, aber das wars leider auch scho

Meflin 18. Feb 2009 11:21

Re: Regex: alle html links eines String bekommen?
 
Wie wärs damit?
Code:
[url="http://[^"](.*?)[/url]
P.S.: meine obige RegEx matcht bei deinem Beispiel [Beitrag #1] auch. KA was du da falsch machst?!

Meflin 18. Feb 2009 12:05

Re: Regex: alle html links eines String bekommen?
 
Oder noch n bissel universeller:
Code:
<a.*?href=['"].*?[^"]+['"].*?>(.*?)</a>

Pseudemys Nelsoni 18. Feb 2009 13:31

Re: Regex: alle html links eines String bekommen?
 
@Meflin, danke schonmal.

Wenn ich auf http://www.nettz.de/Service/regexp/index.cgi den Text überprüfe, bekomme ich 3 Resultate und zwar:

Link1
Link2
Link3


Ich brauche aber den Link selbst, nicht die Beschreibung zu diesem. Hast dafür nochmal nen Code parat?

MfG

PS: Wäre nicht schlecht, wenn du mir jeden Schritt des Patterns einmal erklären könntest, damit ich es nachvollziehen kann.

Danke schonmal.

quendolineDD 18. Feb 2009 13:34

Re: Regex: alle html links eines String bekommen?
 
Einfach das Klammern was du brauchst. Solltest dir vielleicht auch nebenbei noch mal die Hilfe zu RegEx durchlesen ...

Code:
<a.*?href=['"](.*?[^"]+)['"].*?>.*?</a>

Pseudemys Nelsoni 18. Feb 2009 13:36

Re: Regex: alle html links eines String bekommen?
 
Vielen Dank, das funktioniert! :)


Ich benutze C#.NET und dort ist die Hilfe sehr beschränkt was Regexes angeht. Ok ich habe auch schon im Internet geguckt, aber dort ist es wiederum zu Umfangreich (für mich) :).


Aber nun brauch ich sie ja erstmal nicht mehr.

Danke nochmal.

Meflin 18. Feb 2009 19:40

Re: Regex: alle html links eines String bekommen?
 
Hier die Erklärung zur letzten RegEx (mit der für dich richtigen Klammerung):
Code:
<a.*?href=['"](.*?[^"]+)['"].*?>.*?</a>

Match the characters “<a” literally «<a»
Match any single character that is not a line break character «.*?»
   Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
Match the characters “href=” literally «href=»
Match a single character present in the list “'"” «['"]»
Match the regular expression below and capture its match into backreference number 1 «(.*?[^"]+)»
   Match any single character that is not a line break character «.*?»
      Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
   Match any character that is NOT a “"” «[^"]+»
      Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match a single character present in the list “'"” «['"]»
Match any single character that is not a line break character «.*?»
   Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
Match the character “>” literally «>»
Match any single character that is not a line break character «.*?»
   Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
Match the characters “</a>” literally «</a>»


Alle Zeitangaben in WEZ +1. Es ist jetzt 10:45 Uhr.
Seite 1 von 2  1 2      

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz