C# 正则表达式未正确触发

发布于 2024-12-28 15:26:48 字数 1188 浏览 2 评论 0原文

我为 C# (3.5) ASP.NET Web 应用程序编写了一堆正则表达式。无论如何,我都不是高级正则表达式用户。我正在对一个让我完全困惑的问题进行故障排除,因为它为什么不起作用。特别是因为我有类似的工作正常。

以下是详细信息。感谢您的时间和友善的帮助。

我的应用程序所做的是采用 2 个单独的值。如果这两个值一起匹配一组预定义的规则,那么它们就会映射到某个值。

我已经定义了一些这样的规则(注意:这些只是我编造的,没有编程意义或意义):

元素 1:Cxxxx*####

元素 2:Czzzz*####

对于元素 1: 这意味着该序列必须以“C”开头。接下来的 4 个字符(四个 X)是字母数字。星号表示它可以是(“R”或“D”)。 # 符号表示它可以是任意 4 个字母数字字符的序列。

对于元素 2: 这意味着该序列必须以“C”开头。接下来的 4 个字符(四个 Z)是字母数字。星号表示它可以是(“R”或“D”)。 # 符号表示它可以是任意 4 个字母数字字符的序列。

在两个元素之间,X 和 Z 仅仅意味着 4 个字符序列不能相同。两者之间必须是唯一的。

因此,这两个元素违反了规则,因为“1491”在序列中的同一位置重复。

元素 1:C1491D1234

元素 2:C1491D5678

这一个将返回 true,因为考虑到序列规则,它应该没问题:

元素 1:C1491D1234

元素 2:C1599D5678

这一个将返回true,因为考虑到序列规则,它应该没问题:

元素 1:C1491D1234

元素 2: C1599D1234

这是我用来创建此规则的正则表达式:

C([A-Za-z0-9]{4})[DdRr][A-Za-z0-9]{4}-C(?!\ 1)[DdRr][A-Za-z0-9]{4}

这是我用来测试条件的两个元素:

元素 1:C1491D1491

元素2:C1000R4100

由于某种原因,我的正则表达式是错误的并且没有返回 true。

这就是我的 C# 程序如何看待序列:C1491D1491-C1000R4100

我一直在使用此网站进行测试:

http://regexhero。网/测试仪/

I wrote a bunch of regular expressions for a C# (3.5) ASP.NET web application. I am not an advanced regular expression user by any means. I am troubleshooting one that has me totally confused to as why it is not working. Especially since I have similar ones that are working fine.

Here are the details. Thank you for your time and kind assistance.

What my application does is takes 2 individual values. If the 2 values together match a predefined set of rules, then they get mapped to a certain value.

I have defined some of the rules as such (note: these are just made up by me and have no programming significance or meaning):

Element 1: Cxxxx*####

Element 2: Czzzz*####

For Element 1:
What this means is that this sequence has to start with "C". The next 4 characters (the four X's) are alphanumeric. The asterisk means it can be ("R" or "D"). The # symbols mean it can be a sequence of any 4 alphanumeric characters.

For Element 2:
What this means is that this sequence has to start with "C". The next 4 characters (the four Z's) are alphanumeric. The asterisk means it can be ("R" or "D"). The # symbols mean it can be a sequence of any 4 alphanumeric characters.

Between the two elements, the X's and the Z's simply mean that the 4 character sequence cannot be the same. It must be unique between the two.

So these two elements violate the rule since the "1491" repeats in the same spot in the sequence.

Element 1: C1491D1234

Element 2: C1491D5678

This one would return true as it should be fine given the rules of the sequence:

Element 1: C1491D1234

Element 2: C1599D5678

This one would return true as it should be fine given the rules of the sequence:

Element 1: C1491D1234

Element 2: C1599D1234

This is the regular expression I am using to create this rule:

C([A-Za-z0-9]{4})[DdRr][A-Za-z0-9]{4}-C(?!\1)[DdRr][A-Za-z0-9]{4}

These are the two elements I am using to test the condition:

Element 1: C1491D1491

Element 2: C1000R4100

For some reason, my regular expression is wrong and is not returning true.

This is how my C# program sees the sequence: C1491D1491-C1000R4100

I have been using this website to test:

http://regexhero.net/tester/

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

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

发布评论

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

评论(2

皇甫轩 2025-01-04 15:26:49

您缺少匹配第二个括号的第一个字母数字组的部分,

C([A-Za-z0-9]{4})[DdRr][A-Za-z0-9]{4}-C(?!\1)([A-Za-z0-9]{4})[DdRr][A-Za-z0-9]{4} 

与测试字符串匹配。我发现这个正则表达式测试器也很不错:) http://derekslager.com/blog/posts/2007/09/a-better-dotnet-regular-expression-tester.ashx。话虽如此,我确信有一种更有效的方式来编写正则表达式

you are missing the peice to match the second brackets first alphanumeric set,

C([A-Za-z0-9]{4})[DdRr][A-Za-z0-9]{4}-C(?!\1)([A-Za-z0-9]{4})[DdRr][A-Za-z0-9]{4} 

matches the test string. I find this regex tester pretty good too :) http://derekslager.com/blog/posts/2007/09/a-better-dotnet-regular-expression-tester.ashx. Having said this im sure theres a much more effecient way of writing that regex

允世 2025-01-04 15:26:49

你的正则表达式有一个微妙的缺陷。 (?!\1) 是负向前瞻:它检查第一个反向引用是否不重复,但不会消耗新序列。您可以像这样修复您的代码:

@"C([A-Za-z0-9]{4})[DdRr][A-Za-z0-9]{4}-C(?!\1)[A-Za-z0-9]{4}[DdRr][A-Za-z0-9]{4}"

Your regex has a subtle flaw. The (?!\1) is a negative lookahead: It checks that the first backreference does not repeat, but it does not consume the new sequence. You can fix your code like this:

@"C([A-Za-z0-9]{4})[DdRr][A-Za-z0-9]{4}-C(?!\1)[A-Za-z0-9]{4}[DdRr][A-Za-z0-9]{4}"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文