C# 简单字符串正则表达式

C# 简单字符串正则表达式,c#,regex,C#,Regex,我正在尝试编写非常简单的正则表达式-这个世界对我来说是全新的,所以我需要帮助 我需要验证下一个模式:以C0开始,以4位数字结束,例如: C01245 - legal C04751 - legal C15821 - not legal (does not starts with 'C0') C0412 - not legal (mismatch length) C0a457 - not legal 我拿着“备忘单”写下了下一个模式: C0\A\d{4),意思是(我想):以C0开始,然后

我正在尝试编写非常简单的正则表达式-这个世界对我来说是全新的,所以我需要帮助

我需要验证下一个模式:以C0开始,以4位数字结束,例如:

C01245 - legal

C04751 - legal

C15821 - not legal (does not starts with 'C0')

C0412 - not legal (mismatch length)

C0a457 - not legal 
我拿着“备忘单”写下了下一个模式:

C0\A\d{4),意思是(我想):以C0开始,然后以4位数字继续,但是这个模式总是返回“false”

我的模式有什么问题?

如果你去,你可以写这个表达式:

^(C0[0-9]*[0-9]{4})[^0-9]
在你放的内容中:

C012345 - legal
C047851 - legal
C*1*54821 - not legal (does not starts with 'C0')
C0412 - not legal (mismatch length)
C0*a*4587 - not legal
你会发现它只符合你想要的

^C0\d{4,}$
字符串必须以
^
开头,后跟字符串
$
末尾的4个或更多数字
\d{4,}

只要去掉最后一个
$
,如果它实际上不在字符串的末尾

如果你不想在中间夹更多的数字,就去掉逗号


@femtoRgon的
\d{4,}
(请参阅注释)。

您必须使用此正则表达式

^C0\d{4}$
^
将标记字符串的开头

$
将标记字符串的结尾

\d{4}
将匹配4位数字


你也可以这样做

if(input.StartsWith("C0") &&
   input.Length==6 && 
   input.Substring(2).ToCharArray().All(x=>Char.IsDigit(x)))
//valid
else //invalid

请看一下这个片段

using System.IO;
using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
        string input1 = "C0123456"; 
        // input1 starts with C0 and ends with 4 digit , allowing any number of                 
        // characters/digit in between
        string input2 = "C01234";
        // input2 starts with C0 and ends with 4 digit , without                
        // characters/digit in between
        String pattern1=@"\b[C][0][a-z A-Z 0-9]*\d{4}\b";
        String pattern2=@"\b[C][0]\d{4}\b";
        Match m = Regex.Match(input1, pattern1);
        if(m.Success)
        Console.WriteLine("Pattern1 matched input1 and the value is : "+m.Value);
        m = Regex.Match(input2, pattern2);
        if(m.Success)
        Console.WriteLine("Pattern2 matched input2 and the value is : "+m.Value);
          m = Regex.Match(input1, pattern2);
        if(m.Success)
        Console.WriteLine("Pattern2 matched input1 and the value is : "+m.Value);
          m = Regex.Match(input2, pattern1);
        if(m.Success)
        Console.WriteLine("Pattern1 matched input2 and the value is : "+m.Value);


    }
}
输出:

模式1匹配输入1,值为:C0123456

模式2匹配输入2,值为:C01234



模式1匹配输入2,值为:C01234

C0[0-9]{4}
这应该行得通。我认为有两种方法可以阅读您对问题的描述,您的示例与这两种方法中的任何一种都不一致。要么您想要精确的
C0####
,在这种情况下,您的示例都不应该通过,要么您想要
C0--无论您喜欢什么--######
,在这种情况下
C0**a**4587
应该通过我想这已经足够清楚了。我想要的是C0,而不是4位数字[0-9]。C012345-->C0然后12345->这看起来像是C0后面的5位数字,而不是4位数字:d,你可以使用
C0\d{5}
当且仅当由这7个字符组成的字符串
[0-9]
可以替换为
\d
这不一定是个好主意,[0-9]是一种更快的匹配模式,并且还排除了所有unicode定义的数字(如果我们不尝试匹配这些数字,OP建议这样做)。我们可以使用
\d
[0-9]
。两者都是相同的。@格林先生:是的,但是,在这种情况下,我们为什么要选择更详细的方式呢?
\d
是有原因的。为什么不使用它?5个字符对2个字符。我习惯于将其写成[0-9]在java中,因为当我想要使用它时,我必须加倍反斜杠,所以更长的版本对我来说更可读。但是对我来说,新的和有趣的是,从“SpPoCurthTo”问题中得到的评论是一个需要正则表达式的完美例子。我看这里没有理由考虑另一种方法。谢谢你的回答。在你的建议和下一个之间有没有更好的正则表达式:\AC0\d{4}\Z@sfjedi字符串操作有什么问题吗?对我来说似乎很好://格林先生,它真的很冗长。有趣的事实:我相信
\d*\d{4}
可以替换为
\d{4,}< /代码>。这可以更短到<代码> ^ 0\d*$< /代码>。如果假设“代码> C0< /代码>之后的数字是“代码>4</代码>”@绿色不,因为他说它到底需要4位数字。我说首先考虑什么是代码> { 4,} /代码>。当OP需要精确的4位数字时。(只是想知道)@格林先生,这是他的措辞。他说它必须以
C0
开头,以4位数字结尾,但他没有说中间的数字是否无效。