I have already found helpful answers for a regex that matches twitter like username mentions in this answer and this answer
(?<=^|(?<=[^a-zA-Z0-9-_\.]))@([A-Za-z]+[A-Za-z0-9_]+)
(?<=^|(?<=[^a-zA-Z0-9-_\.]))@([A-Za-z]+[A-Za-z0-9-_]+)
However, I need to update this regex to also include usernames that has dots.
- One or more dots are allowed in a username.
- The username must not start or end with a dot.
- No two consecutive dots are allowed.
Example of a matched string:
@valid.user.name
^^^^^^^^^^^^^^^^
Examples of non-matched strings:
@.user.name // starts with a dot
@user.name. // ends with a dot
@user..name // has two consecutive dots