Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String MS SQL-如何从大字符串中获取以逗号/分号分隔的电子邮件地址列表_String_Sql Server 2008_Email_Search_Substring - Fatal编程技术网

String MS SQL-如何从大字符串中获取以逗号/分号分隔的电子邮件地址列表

String MS SQL-如何从大字符串中获取以逗号/分号分隔的电子邮件地址列表,string,sql-server-2008,email,search,substring,String,Sql Server 2008,Email,Search,Substring,如何提取字符串中所有可用电子邮件地址的列表(以逗号/分号分隔的列表) SELECT dbo.getEmailAddresses('this is misc andrew@g.com') --output andrew@g.com SELECT dbo.getEmailAddresses('this is misc andrew@g.com and a medium text returning %John@acme.com') --output andrew@g.com; John@acme

如何提取字符串中所有可用电子邮件地址的列表(以逗号/分号分隔的列表)

SELECT dbo.getEmailAddresses('this is misc andrew@g.com')

--output andrew@g.com

SELECT dbo.getEmailAddresses('this is misc andrew@g.com and a medium text returning %John@acme.com')
--output andrew@g.com; John@acme.com

这是一个UDF。如果您需要其他信息(例如电子邮件地址中的非英语符号),请更正此行
SET@mailtemp='[A-Za-z0-9.-]

试试这个:

create function getEmailAddresses
(
@test varchar(max)
)
returns varchar(max)
As
BEGIN
declare @emaillist varchar(max)
--SET @test=' this is it by it a@b.com dsdkjl dsaldkj a@b.com dasdlk c@bn.com dsafhjkf anand@p.com d fdajf s@s.com .'

;WITH CTE as(
select reverse(left(reverse(left(@test,CHARINDEX('.com',@test)+3)),charindex(' ',reverse(left(@test,CHARINDEX('.com',@test)+3))))) as emailids,
right(@test,len(@test)-(CHARINDEX('.com',@test)+3)) rem
union all
select CASE WHEN len(rem)>2 then reverse(left(reverse(left(rem,CHARINDEX('.com',rem)+3)),charindex(' ',reverse(left(rem,CHARINDEX('.com',rem)+3))))) else 'a' end as emailids ,
CASE WHEN len(rem) > 2 then right(rem,len(rem)-(CHARINDEX('.com',rem)+3)) else 'a' end rem
from CTE where LEN(rem)>2
)
select @emaillist =STUFF((select ','+emailids  from CTE for XML PATH('')),1,1,'')
return @emaillist
END

select dbo.getEmailAddresses('this is it by it a@b.com dsdkjl dsaldkj a@b.com dasdlk c@bn.com dsafhjkf anand@p.com d fdajf s@s.com .')
试试这个

Declare @str varchar(max) = 'this is misc andrew@g.com and a medium text returning John@acme.com'

    ;With Cte AS(
    SELECT
        Items = Split.a.value('.', 'VARCHAR(100)')
    FROM
            (
                SELECT 
                    CAST('<X>' + REPLACE(@str,  ' '  , '</X><X>') + '</X>' AS XML) AS Splitdata  

            ) X  

        CROSS APPLY Splitdata.nodes('/X') Split(a) )

SELECT Email = STUFF((
                SELECT ';'+ Items
                FROM Cte
                Where Items
                LIKE '[A-Z0-9]%[@][A-Z]%[.][A-Z]%'
                FOR XML PATH('')),1,1,'')
注意~您可能需要执行以下操作

a) 根据您的要求,您需要制作一个TVF(表值函数)。你可以参考关于

b) 类似电子邮件验证的条款可以使用,但对于更复杂的需求,您可能必须对其进行增强

c) 如果需要,您可能必须在应用过滤子句之前清理数据。e、 g.%John@acme.com是无效电子邮件。请删除“%”符号,然后应用筛选子句

但正如有人提到的,最好不要在Sql Server端进行太多的字符串拆分/操作,我同意他的观点,因此这里有一个C代码来实现同样的功能

static void Main(string[] args)
        {
            string str = "this is misc andrew@g.com and a medium text returning John@acme.com";

            var result = GetValidEmails(str).Aggregate((a,b)=>a+";" + b);
            Console.WriteLine(result);
            Console.ReadKey();
        }

        private static List<string> GetValidEmails(string input)
        {
            List<string> lstValidEmails = new List<string>();
            string regexPattern = @"^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$";
            foreach (string email in input.Split(' '))
            {
                if (new Regex(regexPattern, RegexOptions.IgnoreCase).IsMatch(email))
                {
                    lstValidEmails.Add(email);
                }
            }
            return lstValidEmails;
        }
static void Main(字符串[]args)
{
string str=“这是杂项andrew@g.com和一个中间文本返回John@acme.com";
var结果=GetValidEmails(str).聚合((a,b)=>a+“;”+b);
控制台写入线(结果);
Console.ReadKey();
}
私有静态列表GetValidEmails(字符串输入)
{
List lstValidEmails=新列表();
字符串regexpatern=@“^[A-Z0-9.\%+-]+@[A-Z0-9.-]+\[A-Z]{2,4}$”;
foreach(input.Split(“”)中的字符串电子邮件)
{
if(新正则表达式(regexpatern,RegexOptions.IgnoreCase).IsMatch(电子邮件))
{
lstValidEmails.Add(电子邮件);
}
}
返回到一个数字;
}

希望这会有所帮助。

这是导入例程的一部分吗?还是商业逻辑?SQL根本不是最好的工具。。。
andrew@g.com;John@acme.com
static void Main(string[] args)
        {
            string str = "this is misc andrew@g.com and a medium text returning John@acme.com";

            var result = GetValidEmails(str).Aggregate((a,b)=>a+";" + b);
            Console.WriteLine(result);
            Console.ReadKey();
        }

        private static List<string> GetValidEmails(string input)
        {
            List<string> lstValidEmails = new List<string>();
            string regexPattern = @"^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$";
            foreach (string email in input.Split(' '))
            {
                if (new Regex(regexPattern, RegexOptions.IgnoreCase).IsMatch(email))
                {
                    lstValidEmails.Add(email);
                }
            }
            return lstValidEmails;
        }