正则表达式从电子邮件中获取域名 [英] Regex get domain name from email
问题描述
我正在学习正则表达式,但无法从电子邮件地址获取 google
I am learning regex and am having trouble getting google
from email address
字符串
first.name@google.com
我只想得到 google,而不是 google.com
I just want to get google, not google.com
正则表达式:
[^@].+(?=\.)
结果:https://regex101.com/r/wA5eX5/1
根据我的理解.它忽略 @
之后找到一个字符串,直到 .
(dot) 使用 (?=\.)
From my understanding. It ignore @
find a string after that until .
(dot) using (?=\.)
我做错了什么?
推荐答案
[^@]
的意思是匹配一个不是@的符号code> 符号.这不是你要找的 - 使用lookbehind
(?<=@)
作为@
和你的(?=\.)
前瞻 \.
以提取中间的服务器名称:
[^@]
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".
这篇关于正则表达式从电子邮件中获取域名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!