【发布时间】:2019-09-30 10:52:45
【问题描述】:
我有这个 SQL 程序集可以从字符串中删除一些 html 标记。对于固定的正则表达式字符串,它工作得非常好,但我想在调用函数时必须将正则表达式字符串作为字符串参数。
我试图将正则表达式作为字符串传递给函数,但它不起作用。我的猜测是我必须以不同的格式传递它。
这是类库。
using System.Data.SqlTypes;
using System.Text.RegularExpressions;
using Microsoft.SqlServer.Server;
public class Functions
{
[SqlFunction]
public static SqlString RemoveHtmlTags(string inText)
{
// Remove html tags other than<p> <b> <h3> <i> <u> <ul> <ol> <li> <strong>
return Regex.Replace(inText, "<\\s*?\\/?(?!(p|b|h3|i|u|ul|ol|li|strong))\\w*\\s*?>", "");
}
}
还有 SQL 函数
CREATE FUNCTION [dbo].[ufn_RemoveHtmlTags](@InputText [nvarchar](max))
RETURNS [nvarchar](max) WITH EXECUTE AS CALLER
AS
EXTERNAL NAME [HtmlTextFunctions].[Functions].[RemoveHtmlTags]
GO
我想要的是:
using System.Data.SqlTypes;
using System.Text.RegularExpressions;
using Microsoft.SqlServer.Server;
public class Functions
{
[SqlFunction]
public static SqlString RemoveHtmlTags(string inText, string regexText)
{
// Remove html tags other than<p> <b> <h3> <i> <u> <ul> <ol> <li> <strong>
return Regex.Replace(inText, regexText, "");
}
}
【问题讨论】:
-
您使用的是哪个 dbms?
-
"but it won't work" 不足以作为错误描述。请添加(通过编辑问题)如何它不起作用。你有任何错误吗?如果是这样,消息是什么?您得到的结果是否与您的预期结果不同?如果是这样,请添加一个示例。
-
我们不建议在 html 上使用 regex,因为 regex 代表正则表达式,而 html 不是正则。它可能适用于有限的应用程序,但并非适用于所有情况。
标签: c# sql parameters .net-assembly