hyperlink regular expression pattern

novice2 sorry at noem.ail
Fri May 9 07:37:22 PDT 2008

John C Wrote:

> Thanks - that seems to extract the href and text.

what you asked - that you got :)
"question is half of  answer" :)

>What about getting other attributes like name and title, as in this link:
> <a href=\"www.google.com\" name=\"googleLink\" title=\"Click Me\">Google Link</a>

imho, it can't be done by one regexp match. because of random sequense of attributes. imho, you should get whole <a> tag attributes string, then iterate attributes in it.
something like this below.
but sorry, it can't catch attributes without quotes.
may be, std.strings non-regexp will be better when parsing attributes.

import std.regexp;
import std.stdio;

void main()
 if (auto m = std.regexp.search("<anothertag><a href=\"www.google.com:8080/dfs?a1=1&a2=2\" name='google Link' color=red title=\"Click Me\"\">This is Google link</a></anothertag>",
   writefln("tag attributes: \"%s\"", m.match(1));
   writefln("tag content: \"%s\"", m.match(2));
   foreach(s; RegExp("(\\S+?)=(['\"]?)(.*?)\\2").search(m.match(1)))
     writefln("found attribute: name=\"%s\", value=\"%s\"", s.match(1), s.match(3));

More information about the Digitalmars-d-learn mailing list