-4

I wrote below Regular Expression and getting this result but in the result I want to exclude say any special character ("@" or "#") etc .

result = re.findall(r"@\w*",text)

text- "Retweets for @cash" Result= @cash Expected - cash

Please guide how can I do this:

I was able to find the correct expression but not able to understand re.findall(r'(?<=@)\w+',text)

joanis
  • 10,635
  • 14
  • 30
  • 40

2 Answers2

0

The correct expression you found uses what is called a zero-width positive lookbehind. (?<=@) says, "there should be @ just before this point", but it does not actually match that text, it just asserts that it should be there.

So, (?<=@)\w+ says "match \w+ with @ just before it".

joanis
  • 10,635
  • 14
  • 30
  • 40
0

(<=@) is a lookbehind assertion. The given input text must match its value, but the assertion does not consume any characters from the input.

Note that you could also transform the result in the host language and strip the leading character:

result = re.findall(r"@\w*",text)[1:]
# or:
result = re.findall(r"@\w*",text).lstrip('@')
knittl
  • 246,190
  • 53
  • 318
  • 364