2

I am new to regular expression module. I am trying to remove all the links in a given exampleString but in one line of code:

exampleSentence = exampleSentence.replace(link for link in re.findall(r'http://*',exampleSentence),'')

But I am getting this syntax error:

SyntaxError: Generator expression must be parenthesized if not sole argument

How to proceed with this?

1
  • I think you are missing the list comprehension square brackets! Can you give you desired input and output clearly! Commented Oct 8, 2015 at 13:24

2 Answers 2

4

You have many issues.

First, str.replace() replace a sub-string by another in a given string; it does not take generators.

Example:

print 'example'.replace('e', 'E')

Next, if you want to remove, there is re.sub():

data = re.sub(
  r'[A-Za-z]+://[A-Za-z0-9-_]+.[A-Za-z0-9-_:%&;\?#/.=]+', # the URI
  '', # the replacement (nothing here)
  input_data
)

The URI regex was copied from @miko-trueman answer.

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

1 Comment

Thank you! Makes things easier :)
2

If all you want to do is remove all links from a string, you don't need a generator. The following will work.

import re
exampleString = "http://google.com is my personal library. I am not one for http://facebook.com, but I am in love with http://stackoverflow.com"
exampleString = re.sub(r"(?:\@|https?\://)\S+", '', exampleString)

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.