简体   繁体   English

相当于MySQL REGEXP的Microsoft SQL Server

[英]Microsoft SQL Server equivalent of MySQL REGEXP

I am trying to reconstruct a database query I created for MySQL in Microsoft SQL Server. 我正在尝试重建为Microsoft SQL Server中的MySQL创建的数据库查询。 I am looking for an operator or function SQL Server which acts like REGEXP . 我正在寻找一种类似于REGEXP的运算符或函数SQL Server。

Here is an example of how I am using the operator: 这是我如何使用运算符的示例:

select *
from   musicdetails
WHERE  artistname REGEXP '^".mysql_escape_string($_GET['search'])."$'

Here you go (compile as SQL CLR assembly): 在这里(编译为SQL CLR程序集):

using System.Collections;
using System.Text.RegularExpressions;
using Microsoft.SqlServer.Server;

public partial class UserDefinedFunctions
{
  [SqlFunction]
  public static bool RegexMatch(string expr, string regex)
  {
    return Regex.IsMatch(expr, regex);
  }

  [SqlFunction]
  public static string RegexReplace(string expr, string regex, string replace)
  {
    return Regex.Replace(expr, regex, replace);
  }

  [SqlFunction(FillRowMethodName="GetToken", 
       TableDefinition="Value nvarchar(max)")]
  public static IEnumerable RegexSplit(string expr, string regex)
  {
    return Regex.Split(expr, regex);
  }

  public static void GetToken(object row, out string str)
  {
     str = (string) row;
  }
}

The only way you can do this in SQL Server (2005 and up only) is to use CLR functions; 在SQL Server(仅限2005年及更高版本)中执行此操作的唯一方法是使用CLR函数。 regular expressions as part of native SQL queries isn't standard. 作为本机SQL查询一部分的正则表达式不是标准的。

http://msdn.microsoft.com/en-us/magazine/cc163473.aspx http://msdn.microsoft.com/en-us/magazine/cc163473.aspx

While the code in leppie's answer will compile and execute, I would not recommend it for production use. 虽然leppie答案中的代码可以编译和执行,但我不建议将其用于生产。 If you want RegEx functions that: 如果要使用RegEx函数,请执行以下操作:

  • are coded using best practices (for performance and security) 使用最佳做法进行编码(出于性能和安全性考虑)
  • handle NULL s appropriately 适当地处理NULL
  • perform better / more efficient 表现更好/更高效
  • have more options (eg @StartAt , @RegExOptions for IgnoreCase , MultiLine , etc) 有更多选项(例如, IgnoreCase的 @StartAt@RegExOptionsMultiLine等)
  • provide more functionality (eg CaptureGroup , CaptureGroupCapture , Escape , Unescape , etc) 提供更多的功能(例如CaptureGroup,CaptureGroupCapture, 逃生 ,UNESCAPE等)
  • require no additional effort to deploy (ie a single, self-contained T-SQL script that is portable and versionable, and installs cleanly with no need to manually enable "CLR enabled" or mess with the "CLR strict security" setting that was introduced in SQL Server 2017 ) 不需要额外的精力来部署(即,一个可移植且可版本控制的独立的T-SQL脚本,并且可以干净地安装,而无需手动启用“启用了CLR”或使用引入“ CLR严格安全性”设置在SQL Server 2017中

and are also free, then check out the SQL# SQLCLR library (that I wrote). 并且也是免费的,然后签出SQL# SQLCLR库(我写的)。 There are 13 RegEx functions in the Free version (and 2 more in the Full / paid version, plus the ability to increase the expression cache size, which can help if you frequently use a variety of expressions). 免费版中有13个RegEx函数(完全版/付费版中还有2个RegEx函数,以及增加表达式缓存大小的功能,如果您经常使用各种表达式,则可以提供帮助)。

I believe the function RegEx_IsMatch (or RegEx_IsMatch4k ) is what you are looking for (yes, it is in the Free version). 我相信您正在寻找功能RegEx_IsMatch (或RegEx_IsMatch4k )(是的,它在免费版本中)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM