0

I'm looking for substring starting with @ and ending with the first \s occurrence. It's necessary to have @ at the beginning of the string or after space.

Example: @one bla bla bla @two @three@four #@five

Result: @one, @two, @three@four

I end up with this re: ((?<=\s)|(?<=^))@[^\s]+ which works fine in sublime text 2, but returns empty strings in python.

python code:

re.findall(r'((?<=^)|(?<=\s))@[^\s]+', '@one bla bla bla @two @three@four #@five')
KarSho
  • 5,699
  • 13
  • 45
  • 78
qubblr
  • 45
  • 4

2 Answers2

2

if you are willing not to use reg expr you could try:

>>> s ="@one bla bla bla @two @three@four #@five"
>>> filter(lambda x:x.startswith('@'), s.split())
['@one', '@two', '@three@four']

This actually should be much faster...

vkontori
  • 1,187
  • 1
  • 10
  • 17
0

Your capturing group isn't capturing the text that you are really looking for:

(?:(?<=^)|(?<=\s))(@[^\s]+)

Now, it works:

>>> re.findall(r'(?:(?<=^)|(?<=\s))(@[^\s]+)', '@one bla bla bla @two @three@four #@five')
['@one', '@two', '@three@four']
Blender
  • 289,723
  • 53
  • 439
  • 496
  • It's worth mentioning that the reason for this behavior is that if capturing groups are present, `findall` returns them instead of returning the entire match (even though it *does* return the entire match if there are no groups). This is documented but it always seems to surprise people. – BrenBarn Dec 21 '12 at 06:48