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?

psr
  • 2,619
  • 4
  • 32
  • 57
  • I think you are missing the list comprehension square brackets! Can you give you desired input and output clearly! – Vivek Oct 08 '15 at 13:24

2 Answers2

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.

Community
  • 1
  • 1
bufh
  • 3,153
  • 31
  • 36
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)
James Jeffery
  • 12,093
  • 19
  • 74
  • 108