.net c#正则表达式中的单词单词不能正确失败

Regex for integer in the word not failing properly
2021-09-15
  •  译文(汉语)
  •  原文(英语)

我正在尝试创建正则表达式以完全匹配一种字符串格式.琴弦会像这样


A3476,TextA
B5628,TextB
A9871,TextC

第一个字符应该是"A"或"B",并且将跟随整数,长度应恰好为4个字符,后跟",".逗号后仅重复三个单词,即"TextA"或"TextB"或"TextB".

我已经尝试过此正则表达式


(A|B)(\d{4})(,)(TextA|TextB|TextC)

当我在整数中添加任何字母或长度大于4个字符的整数时,字符串匹配应会失败,但不会失败.

假设字符串是这样的


A653k7876,TextA

我得到这样的结果7876,TextA.结果缺少字符"A",并从末尾读取整数.我的意图是失败.

解决过程1

您的正则表达式是可以的,除了您应该指出匹配的开始和结束位置.该^特殊字符指示行或字符串,并开始$结束.因此,请尝试以下操作:

^(A|B)(\d{4})(,)(TextA|TextB|TextC)$

确保RegexOptions.Multiline在创建Regex对象时指定以使其工作.

速聊1:
谢谢解决.我正在尝试使用Singleline选项,并且没有'^'符号.
解决过程2

使用:

\b(A|B)(\d{4}),(TextA|TextB|TextC)\b

\ b表示单词边界

您可能会发现此链接有用:C#Regex备忘单

解决过程3

看起来我们需要更多信息,好像我完全按照您的要求使用字符串一样工作正常:

Console.WriteLine(Regex.IsMatch(@"A653k7876,TextA", @"(A|B)(\d{4})(,)(TextA|TextB|TextC)"));

输出False-根据需要.

I am trying to create regex to exactly match one string format. The strings will be like this


A3476,TextA
B5628,TextB
A9871,TextC

The first character should be either 'A' or 'B' and it will follow integer number which should be exactly 4 chars in length and followed by ','. After comma only Three words will repeate those are either 'TextA' or 'TextB' or 'TextB'.

I have tried this regex


(A|B)(\d{4})(,)(TextA|TextB|TextC)

When I add any alphabet in the integer number or integer number is greater than 4 chars in length the string match should fail but it is not failing.

Suppose if the string is like this


A653k7876,TextA

I am getting result like this 7876,TextA. The result is missing character 'A' and reading integer from end. My intention is it should fail.

Solutions1

Your regex is fine, except that you should indicate where it should start and end with the match. The ^ special character indicates the start of a line or string, and $ the end. So, try this instead:

^(A|B)(\d{4})(,)(TextA|TextB|TextC)$

Make sure you specify RegexOptions.Multiline when creating the Regex object to make this work.

Talk1:
Thanks it solved. I was trying with Singleline option and without '^' symbol.
Solutions2

Use:

\b(A|B)(\d{4}),(TextA|TextB|TextC)\b

\b denotes the word boundary

You might find this link useful: C# Regex Cheat Sheet

Solutions3

Looks like we need more info, as if I use strings exactly as you provided they work fine:

Console.WriteLine(Regex.IsMatch(@"A653k7876,TextA", @"(A|B)(\d{4})(,)(TextA|TextB|TextC)"));

outputs False - as you want.

转载于:https://stackoverflow.com/questions/15544429/regex-for-integer-in-the-word-not-failing-properly

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入