Regex 在@sign之后提取用户名或电子邮件/域
我有一个包含用户名和电子邮件地址列表的文件,我需要两个表达式。一个获取电子邮件地址(它们总是以.com或.net或.org结尾),另一个获取用户名 我只希望用户名作为一个表达式和域部分作为另一个,我不希望使用Regex 在@sign之后提取用户名或电子邮件/域,regex,Regex,我有一个包含用户名和电子邮件地址列表的文件,我需要两个表达式。一个获取电子邮件地址(它们总是以.com或.net或.org结尾),另一个获取用户名 我只希望用户名作为一个表达式和域部分作为另一个,我不希望使用@sign @stackoverflow.com @google.com @example.com 我试过了 ^@.*?..*?$ 使用者 我试过了 ^@.*?$ 任何建议都很好。您可以对域执行以下操作: ^@[^.]+\.[^.]+$ 这将匹配字符串的开头,后跟一个@,后跟一个或多
@
sign
@stackoverflow.com
@google.com
@example.com
我试过了
^@.*?..*?$
使用者
我试过了
^@.*?$
任何建议都很好。您可以对域执行以下操作:
^@[^.]+\.[^.]+$
这将匹配字符串的开头,后跟一个@
,后跟一个或多个除
以外的任何字符,后跟一个
,后跟一个或多个除
以外的任何字符,后跟字符串的结尾
但这不会捕获包含两个以上部分的域(例如@meta.stackoverflow.com
)。如果这很重要,您可以尝试以下方法:
^@[^.]+(\.[^.]+)+$
这将匹配字符串的开头,后跟一个@
,后跟一个或多个除
以外的任何字符,后跟一个由
组成的组,后跟一个或多个除
以外的任何字符,其中该组可以重复一次或多次,后跟字符串的末尾
这是为用户准备的:
^@[^.]+$
这将匹配字符串的开头,后跟一个
@
,后跟除
以外的一个或多个字符,后跟字符串的结尾。在第一个表达式中,如果在最后一个*?
之前转义了点\。
,那么它将匹配。第二个表达式正好与整行匹配。要匹配但排除@
,可以执行以下操作
对于域,请使用:
^@(\S+\.[^\s]+)$
正则表达式:
^ the beginning of the string
@ '@'
( group and capture to \1:
\S+ non-whitespace (all but \n, \r, \t, \f, and " ") (1 or more times)
\. '.'
[^\s]+ any character except: whitespace (1 or more times)
) end of \1
$ before an optional \n, and the end of the string
^ the beginning of the string
@ '@'
( group and capture to \1:
[^\s.]+ any character except: whitespace or '.' (1 or more times)
) end of \1
$ before an optional \n, and the end of the string
看
供用户使用:
^@([^\s.]+)$
正则表达式:
^ the beginning of the string
@ '@'
( group and capture to \1:
\S+ non-whitespace (all but \n, \r, \t, \f, and " ") (1 or more times)
\. '.'
[^\s]+ any character except: whitespace (1 or more times)
) end of \1
$ before an optional \n, and the end of the string
^ the beginning of the string
@ '@'
( group and capture to \1:
[^\s.]+ any character except: whitespace or '.' (1 or more times)
) end of \1
$ before an optional \n, and the end of the string
请参见试试这个
用户
(?这与下一行的@匹配吗?