0

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...
1
  • Why not r'/url\?q=(http://.*?)&sa=.*'? Commented Feb 25, 2014 at 9:37

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.