如何提取字符串中所有可用电子邮件地址的列表(以逗号/分号分隔的列表形式)
SELECT dbo.getEmailAddresses('this is misc <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d2b3bcb6a0b7a592b5fcb1bdbf" rel="noreferrer noopener nofollow">[email protected]</a>')
--output <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a4c5cac0d6c1d3e4c38ac7cbc9" rel="noreferrer noopener nofollow">[email protected]</a>
SELECT dbo.getEmailAddresses('this is misc <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9bfaf5ffe9feecdbfcb5f8f4f6" rel="noreferrer noopener nofollow">[email protected]</a> and a medium text returning %<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5a103532341a3b39373f74393537" rel="noreferrer noopener nofollow">[email protected]</a>')
--output <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d5b4bbb1a7b0a295b2fbb6bab8" rel="noreferrer noopener nofollow">[email protected]</a>; <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5f153037311f3e3c323a713c3032" rel="noreferrer noopener nofollow">[email protected]</a>
最佳答案
这是一个用于此目的的 UDF。如果您需要其他内容(例如电子邮件地址中的非英语符号),请更正此行 SET @MailTempl='[A-Za-z0-9_.-]
。 Here is a full list of allowed characters
CREATE FUNCTION [dbo].[getEmailAddresses] (@Str varchar(8000))
RETURNS varchar(8000) AS
BEGIN
declare @i int, @StartPos int,@AtPos int,@EndPos int;
declare @MailList varchar(8000);
declare @MailTempl varchar(100);
SET @MailList=NULL;
SET @MailTempl='[A-Za-z0-9_.-]'; --allowing symbols in e-mail not including @
SET @AtPos=PATINDEX('%'+@MailTempl+'@'+@MailTempl+'%',@Str)+1;
While @AtPos>1
begin
--go left
SET @i=@AtPos-1;
while (substring(@Str,@i,1) like @MailTempl) SET @i=@i-1;
SET @StartPos=@i+1;
--go right
SET @i=@AtPos+1;
while (substring(@Str,@i,1) like @MailTempl) SET @i=@i+1;
SET @EndPos=@i-1;
SET @MailList=isnull(@MailList+';','')+Substring(@Str,@StartPos,@EndPos-@StartPos+1);
--prepare for the next round
SET @Str=substring(@Str,@EndPos+1,LEn(@Str));
SET @AtPos=PATINDEX('%'+@MailTempl+'@'+@MailTempl+'%',@Str)+1;
end;
RETURN @MailList;
END
关于string - MS SQL - 如何获取从大字符串中搜索的逗号/分号分隔的电子邮件地址列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12085326/