'RegEx for twitter usernames

I need some help with regex for usernames which start with "@" and following rules:

  1. Username includes only \w symbols.

  2. Match if string has any non word character except "@", e.g.

    • Match @username in @username!&?()*^
    • But don't match in @username@username, @username!@username or @username!%^@ (if string has at least one more "@").
  3. Don't match if string has any symbols before "@", e.g.

    • Don't match @username in not@username or !@username.

For now i have:

(?<!\w)@(\w{4,15})\b(?!@)

Which excludes only \w symbols before and "@" if it stands only after username.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source