Regex to extract hyperlink containing a specific word -
i need extract hyperlink, containing specific word in url, piece of text. example;
"this text link page. click link <a href="/server/specificword.htm>this link page</a> see page. here link doesn't have word "specificword" in it: <a href="/server/mypage.htm>this link without word "specificword" in url</a>"
so, need parse text, check hyperlinks see if 1 of them contains word "specificword", , extract entire hyperlink. end this:
<a href="/server/specificword.htm>this link page</a> i need hyperlink has specificword in url eg. /server/specificword.htm, not in link text
one regex have tried, one: /(<a[^>]*>.*?</a>)|specificword/ match hyperlinks in text, or "specificword". if text has multiple links, without word "specificword", too.
also, have tried one, matces nothing:
<a.*?href\s*=\s*["\']([^"\'>]*specificword[^"\'>]*)["\'][^>]*>.*?<\/a> my regex skills end here, great....
try tag:
/<a [^>]*\bhref\s*=\s*"[^"]*specificword.*?<\/a>/ or link (in first capture group):
/<a [^>]*\bhref\s*=\s*"([^"]*specificword[^"]*)/ if use php, link:
preg_match_all('/<a [^>]*\bhref\s*=\s*"\k[^"]*specificword[^"]*/', $text, $results);
Comments
Post a Comment