Sanjay Khatri
Sanjay Khatri

Reputation: 4211

regular expression: Find url from anchor tag

i have problem . i want to find out the url form acnchor tag which consist "title" tag in anchor tag.

example:
<a href="http://www.test.com" title="xyz">this is test</a>

how can i match the string and fetch url using regular expression.

thanks

Upvotes: 0

Views: 6792

Answers (3)

Biroka
Biroka

Reputation: 609

/href="(.*?)(?=".*?title)/

you will have to trim the href=" from the beginning of the match

/abc(?=xyz)/ positive lookahead -> matches abc if abc is followed by xyz

Upvotes: 0

Ties
Ties

Reputation: 5846

<a\s+([^>]*)href="(https?:\/\/([^"]*))"\s+([^>]*)title="xyz"(.*?)>(.*?)<\/a> you can get the url by partial match $2, you can try it here

Upvotes: 4

Daniel Egeberg
Daniel Egeberg

Reputation: 8382

To be honest, I would use an HTML parsing library to just get the contents of the href attribute.

Upvotes: 2

Related Questions