user
user

Reputation: 141

Extract only the link from string using regex

I want to extract link from this below mentioned string.

 str = /url?q=http://www.example.com/services/blog/first-article&sa=U&ei...

I used the following regular expression to get that link.But it fetches the full url after "http" because I mentioned the pattern to be.What I want is to get only URL before the pattern "&sa" (ie) "http://www.example.com/services/blog/first-article"

 links = re.findall(r'/url\?q=(http://.*)', str)
 print links  # http:example.com/services/blog/first-article&sa=U&ei...

Upvotes: 0

Views: 114

Answers (1)

isedev
isedev

Reputation: 19631

This is the regular expression you need:

links = re.findall(r'/url\?q=(http://[^&]*)', str)

In words: get everything after /url?q=, starting with http:// and which doesn't contain a & character.

Upvotes: 2

Related Questions