Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 正则表达式来查找字符串是否先有一个字符,然后是数字_C#_Regex - Fatal编程技术网

C# 正则表达式来查找字符串是否先有一个字符,然后是数字

C# 正则表达式来查找字符串是否先有一个字符,然后是数字,c#,regex,C#,Regex,谁能告诉我以下字符串格式应该使用哪种模式: 第一个字符始终为“C”或“C” 之后是1-4个数字 然后‘’ 最后又一系列的一些字符号没有固定 例如:C10_COM、C1122_ABC等 在c for Regex.IsMatch中尝试以下操作: ^[cC][0-9]{1,4}_.*$ 其中: ^ = Start of the line [cC] = either upper or lowercase c [0-9]{1,4] = Match a number 1 to 4 times _ = un

谁能告诉我以下字符串格式应该使用哪种模式:

第一个字符始终为“C”或“C” 之后是1-4个数字 然后‘’ 最后又一系列的一些字符号没有固定 例如:C10_COM、C1122_ABC等

在c for Regex.IsMatch中尝试以下操作:

^[cC][0-9]{1,4}_.*$
其中:

^ = Start of the line
[cC] = either upper or lowercase c
[0-9]{1,4] = Match a number 1 to 4 times
_ = underscore
.* = Any number of characters
$ = end of line

附录:您没有指定是否允许在行尾有零个字符。如果没有,则将。*替换为?*。

您能分享您迄今为止的尝试吗?嗨,丹尼尔。。我试过这个方法。。但从未使用过正则表达式。所以我只是想得到正确的答案。我不知道为什么我得了-5分这个问题似乎离题了,因为它不太可能帮助网站的更多访问者。非常感谢David提供的解决方案和您的解释。它帮助我以比文档更好的方式理解它。@Littlemissan解释总是比回答好1000倍。给一个人一条鱼…完全同意你!再次感谢!简洁但足够详细,+1: