Use
.*\B@(?=\w{5,32}\b)[a-zA-Z0-9]+(?:_[a-zA-Z0-9]+)*.*
See proof
\B
before @
means there must be a non-word character or start of string right before the @
.
EXPLANATION
NODE EXPLANATION
--------------------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
\B the boundary between two word chars (\w)
or two non-word chars (\W)
--------------------------------------------------------------------------------
@ '@'
--------------------------------------------------------------------------------
(?= look ahead to see if there is:
--------------------------------------------------------------------------------
\w{5,32} word characters (a-z, A-Z, 0-9, _)
(between 5 and 32 times (matching the
most amount possible))
--------------------------------------------------------------------------------
\b the boundary between a word char (\w)
and something that is not a word char
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
[a-zA-Z0-9]+ any character of: 'a' to 'z', 'A' to 'Z',
'0' to '9' (1 or more times (matching the
most amount possible))
--------------------------------------------------------------------------------
(?: group, but do not capture (0 or more times
(matching the most amount possible)):
--------------------------------------------------------------------------------
_ '_'
--------------------------------------------------------------------------------
[a-zA-Z0-9]+ any character of: 'a' to 'z', 'A' to
'Z', '0' to '9' (1 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
)* end of grouping
--------------------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
.*[^a-zA-Z]@
... which would be far from perfect. Then I looked up emailregex.com And thought... maybe that would be helpful? You could maybe get your match as you have it, then use another regex to check if the "username" is actually a username, or if it's an email. – Wakerly