简体   繁体   English

C#正则表达式匹配太慷慨

[英]c# Regex matches too generous

I need to confirm the format of a customer number, which needs to be the format of #####-## (# being a 0-9 digit and the dash being a literal dash). 我需要确认客户编号的格式,该编号必须为#####-##的格式(#为0-9数字,而破折号为文字破折号)。

I built the regex using RegexBuddy (which lets you type in test strings to ensure it is right). 我使用RegexBuddy构建了正则表达式(可让您输入测试字符串以确保它正确)。 I ended up with: 我最终得到了:

\d{5}-\d{2}

This tests well, giving me the desired outcomes - a 'hit' on: 这测试很好,给了我想要的结果-受到“打击”:

12345-01

and no match for 而且没有匹配

12345

This tool provides the code to use this regex in various languages - I want to use this in C# to return true for a match against the entire string. 该工具提供了以多种语言使用此正则表达式的代码-我想在C#中使用此正则表达式为与整个字符串的匹配返回true。 This gives me the below, which I have put into code. 这给了我下面的内容,我已经将它们放入了代码中。

Regex.IsMatch(c.Bill_To, @"\A\d{5}-\d{2}\Z")

This code however returns my 12345 as a match! 但是此代码返回我的12345作为匹配项!

Have I done something wrong? 我做错什么了吗?

Your RegEx works correctly. 您的RegEx可以正常工作。 Try checking the c.Bill_To value. 尝试检查c.Bill_To值。

        bool testResult;

        var testSuccess = "12345-01";
        testResult = Regex.IsMatch(testSuccess, @"\A\d{5}-\d{2}\Z"); //is True

        var testFail = "12345";
        testResult = Regex.IsMatch(testFail, @"\A\d{5}-\d{2}\Z"); //is False

I think I see the issue your are having. 我想我看到您遇到的问题。 If one of your customer numbers is in a string along with additional content, then the pattern you have isn't working. 如果您的客户编号之一与其他内容一起包含在字符串中,则您使用的模式无效。 Specifying both \\A and \\Z means your customer number must start at the beginning of the string and end at the end of the string in order to be matched. 同时指定\\A\\Z意味着您的客户编号必须在字符串的开头开始,在字符串的末尾结束才能匹配。 \\A is like ^ which matches the beginning of a string, and \\Z is like $ which matches the end of a string -- except that they ignore whether the MultiLine option was specified. \\A类似于^ ,它匹配字符串的开头, \\Z类似于$ ,它匹配字符串的结尾-区别在于它们忽略是否指定了MultiLine选项。

Try using this pattern: \\b\\d{5}-\\d{2}\\b 尝试使用以下模式: \\b\\d{5}-\\d{2}\\b

It will only match your customer numbers if they are on a boundary between alphanumeric and non-alphanumeric characters. 仅当您的客户编号位于字母数字字符和非字母数字字符之间时,它才会与您的客户数字匹配。 That also means you can find customer numbers even if they aren't separated by just whitespace as shown in the last test case in the following LINQPad script. 这也意味着即使下面的LINQPad脚本中的最后一个测试用例中所示,即使客户号之间不只是空格,也可以找到它们。

const string pattern = @"\b\d{5}-\d{2}\b";

Regex.IsMatch("12345", pattern).Dump(); // no match
Regex.IsMatch("12345-12", pattern).Dump(); // match
Regex.IsMatch("12345-1234", pattern).Dump(); // no match
Regex.IsMatch("word 12345 word", pattern).Dump(); // no match
Regex.IsMatch("word 12345-12 word", pattern).Dump(); // match
Regex.IsMatch("word 12345-1234 word", pattern).Dump(); // no match
Regex.IsMatch("word@12345-12@word", pattern).Dump(); // match

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

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