0

So I have found this regex:

(?<=^|(?<=[^a-zA-Z0-9-_\.]))@([A-Za-z]+[A-Za-z0-9-_]+)

from this question: regex for Twitter username

But this ignores the actual "@" in the handle when returning. I've tried to edit this to include @ without success. I want to return "@username" instead of "username". How do I edit this regex to include "@"?

BananaBackend
  • 95
  • 1
  • 1
  • 13

1 Answers1

2

You basically need to put @ inside the capturing group you are returning. However, the pattern is highly cryptic and can be greatly simplified.

(?<![\w.-])@[A-Za-z][\w-]+

See the regex demo

Details

  • (?<![\w.-]) - a negative lookbehind that fails the match if, immediately to the left of the current location, there is a word char, or . or -
  • @ - a @ char
  • [A-Za-z] - an ASCII letter
  • [\w-]+ - 1 or more word chars or hyphens.

In Python 3, compile the pattern with re.ASCII flag to make \w only match ASCII letters and digits.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563