Question

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')
Was it helpful?

Solution 2

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']

OTHER TIPS

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...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top