Dataview RowFilter 转义无效字符

发布于 2024-11-16 17:19:20 字数 411 浏览 2 评论 0原文

是否有人有一种方便的现成方法来转义通配符,字符串中的无效字符要分配给 C# 中 DataViewRowFilter 。规则有点像下面,

If a column name contains any of these special characters ~ ( ) # \ / = > < + - * % & | ^ ' " 

[ ], you must enclose the column name within square brackets [ ]. If a column name contains 

right bracket ] or backslash \, escape it with backslash (\] or \\).

谢谢:)

Does any one have a handy ready to use method to escape wildcards, invalid characters in the string to be assigned to RowFilter for a DataView in C#. The rules somewhat are like below

If a column name contains any of these special characters ~ ( ) # \ / = > < + - * % & | ^ ' " 

[ ], you must enclose the column name within square brackets [ ]. If a column name contains 

right bracket ] or backslash \, escape it with backslash (\] or \\).

thanks :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

飘过的浮云 2024-11-23 17:19:20

这将基于这里找到的更详细的规范来工作,

protected void Page_Load(object sender, EventArgs e)
{
    CheckValue("fefe[][]12#");
    CheckValue("abvds");
    CheckValue("#");
    CheckValue(@"[][][][][]\\\\\][]");
    CheckValue("^^^efewfew[[]");
}

public static string CheckValue(string value)
{
    StringBuilder sBuilder = new StringBuilder(value);

    string pattern = @"([-\]\[<>\?\*\\\""/\|\~\(\)\#/=><+\%&\^\'])";

    Regex expression = new Regex(pattern);

    if (expression.IsMatch(value))
    {
        sBuilder.Replace(@"\", @"\\");
        sBuilder.Replace("]", @"\]");
        sBuilder.Insert(0, "[");
        sBuilder.Append("]");
    }
    return sBuilder.ToString();
}

我把它扔掉了进入我正在查看的网页,因此忽略 Page_Load,当然可以根据需要进行测试。

希望有帮助。

This will work based on the specs outlined in a bit more detail found here

protected void Page_Load(object sender, EventArgs e)
{
    CheckValue("fefe[][]12#");
    CheckValue("abvds");
    CheckValue("#");
    CheckValue(@"[][][][][]\\\\\][]");
    CheckValue("^^^efewfew[[]");
}

public static string CheckValue(string value)
{
    StringBuilder sBuilder = new StringBuilder(value);

    string pattern = @"([-\]\[<>\?\*\\\""/\|\~\(\)\#/=><+\%&\^\'])";

    Regex expression = new Regex(pattern);

    if (expression.IsMatch(value))
    {
        sBuilder.Replace(@"\", @"\\");
        sBuilder.Replace("]", @"\]");
        sBuilder.Insert(0, "[");
        sBuilder.Append("]");
    }
    return sBuilder.ToString();
}

I tossed it into a web page I was looking at so disregard the Page_Load and of course test however you would like.

Hope that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文