gpt4 book ai didi

c# - 用于验证字符串仅包含 0 - 9、+、#、*、[ 和 ] 的正则表达式

转载 作者:太空狗 更新时间:2023-10-29 23:00:42 25 4
gpt4 key购买 nike

在我的 C# 应用程序中,我需要验证字符串以确保它们仅包含以下内容:

  • 0 - 9
  • +
  • #
  • *
  • [
  • ]

当我的用户编辑 DataGridView 控件的单元格中的字符串字段时,我需要验证该值。我的 CellValidating 事件处理程序当前如下所示:

if (!Regex.IsMatch(e.FormattedValue.ToString(), @"\A\b[0-9]+\b\Z"))
{
// notify the user that the string is invalid and cancel validation
e.Cancel = true;
}

这似乎适用于 0 - 9,但我还没有得到一个包含我需要的所有元字符的正则表达式。我尝试一次向现有正则表达式添加一个元字符,但它不起作用。例如……

if (!Regex.IsMatch(e.FormattedValue.ToString(), @"\A\b[0-9#]+\b\Z"))

...不允许 # 像我想的那样。逃避它也没有什么不同。任何人都可以为我阐明这一点吗?

最佳答案

使用这个正则表达式 ^[0-9+#*\[\]]+$

关于c# - 用于验证字符串仅包含 0 - 9、+、#、*、[ 和 ] 的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13975157/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com