SQL Server中使用Regex

來源:互聯網
上載者:User

標籤:

原文地址:http://www.cnblogs.com/colder/p/3796864.html

SQL Server 2005及以上版本支援用CLR語言(C# .NET、VB.NET)編寫過程、觸發器和函數,因此使得正則匹配,資料提取能夠在SQL中靈活運用,大大提高了SQL處理字串,文本等內容的靈活性及高效性。

操作步驟:

1.建立一個SQL Server項目(輸入使用者名稱,密碼,選擇DB),建立好後,可以在屬性中更改的

2.建立一個類“RegexMatch.cs”,選擇使用者定義的函數

可以看到,該類為一個部分類:public partial class UserDefinedFunctions

現在可以在該類中寫方法了,注意方法的屬性為:[Microsoft.SqlServer.Server.SqlFunction]

現在類中增加以下兩個方法:

/// 是否匹配Regex    /// </summary>    /// <param name="input">輸入的字串</param>    /// <param name="pattern">Regex</param>    /// <param name="ignoreCase">是否忽略大小寫</param>    /// <returns></returns>    [Microsoft.SqlServer.Server.SqlFunction]    public static bool RegexMatch(string input, string pattern, bool ignoreCase)    {        bool isMatch = false;        if (!string.IsNullOrEmpty(input) && !string.IsNullOrEmpty(pattern))        {            try            {                Match match = null;                if (ignoreCase)                    match = Regex.Match(input, pattern, RegexOptions.Multiline | RegexOptions.IgnoreCase | RegexOptions.Compiled);                else                    match = Regex.Match(input, pattern, RegexOptions.Multiline | RegexOptions.Compiled);                if (match.Success)                    isMatch = true;            }            catch { }        }        return isMatch;    }    /// 擷取Regex分組中的字元    /// </summary>    /// <param name="input">輸入的字串</param>    /// <param name="pattern">Regex</param>    /// <param name="groupId">分組的位置</param>    /// <param name="maxReturnLength">返回字元的最大長度</param>    /// <returns></returns>    [Microsoft.SqlServer.Server.SqlFunction]    public static string GetRegexMatchGroups(string input, string pattern, int groupId, int maxReturnLength)    {        string strReturn = string.Empty;        if (!string.IsNullOrEmpty(input) && !string.IsNullOrEmpty(pattern))        {            try            {                Match match = Regex.Match(input, pattern, RegexOptions.Multiline | RegexOptions.IgnoreCase | RegexOptions.Compiled);                if (match.Success && (groupId < match.Groups.Count))                {                    strReturn = match.Groups[groupId].Value;                    strReturn = (strReturn.Length <= maxReturnLength) ? strReturn : strReturn.Substring(0, maxReturnLength);                }            }            catch            {                return string.Empty;            }        }        return strReturn;    }

3.下一步就是部署的問題了,點擊項目右鍵--》部署即可

提示部署成功了,可以在資料庫的純量值函式中多了這兩個方法了。

使用方法和正常的SQL函數一樣:

select dbo.RegexMatch(‘/Book/103.aspx‘,‘/book/(\d+).aspx‘,‘true‘)
訊息 6263,層級 16,狀態 1,第 1 行
禁止在 .NET Framework 中執行使用者代碼。啟用 "clr enabled" 配置選項。

——出現此錯誤,配置下:

exec sp_configure ‘clr enabled‘,1;
reconfigure with override;

是否匹配:

select dbo.RegexMatch(‘/Book/103.aspx‘,‘/book/(\d+).aspx‘,1)

返回1,表示匹配成功

select dbo.RegexMatch(‘/Book/103.aspx‘,‘/book/(\d+).aspx‘,0)

表示0,匹配失敗(不忽略大小寫)。

資料提取:

select dbo.GetRegexMatchGroups(‘/Book/103.aspx‘,‘/book/(\d+).aspx‘,1,50)

返回103,非常方便的提取。


注意:SQL中使用CLR時,盡量使用try catch…以免出現異常。

SQL Server中使用Regex

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.