Regex get domain name from email
This should be the regex:
(?<=@)[^.]+
(?<=@)
- places the search right after the @
[^.]+
- take all the characters that are not dot (stops on dot)
So it extracts google
from the email address.
I used the solution's regex for my task, but realized that some of the emails weren't that easy: [email protected]
, [email protected]
, and[email protected]
To anyone who came here wanting the sub domain as well (or is being cut off by it), here's the regex:
(?<=@)[^.]*.[^.]*(?=\.)
Updated answer:
Use a capturing group and keep it simple :)
@(\w+)
Explanation by splitting it up(
capturing group for extraction )
\w
stands for word character [A-Za-z0-9_]
+
is a quantifier for one or more occurances of \w
Regex explanation and demo on Regex101
[^@]
means "match one symbol that is not an @
sign. That is not what you are looking for - use lookbehind (?<=@)
for @
and your (?=\.)
lookahead for \.
to extract server name in the middle:
(?<=@)[^.]+(?=\.)
The middle portion [^.]+
means "one or more non-dot characters".
Demo.