[英]Search for 2 specific letters followed by 4 numbers Regex
I need to check if a string begins with 2 specific letters and then is followed by any 4 numbers. 我需要检查一个字符串是否以2个特定字母开头,然后是任意4个数字。
the 2 letters are "BR" so BR1234 would be valid so would BR7412 for example. 2个字母为“ BR”,因此BR1234有效,例如BR7412也有效。
what bit of code do I need to check that the string is a match with the Regex in C#? 我需要什么代码来检查字符串是否与C#中的Regex匹配?
the regex I have written is below, there is probably a more efficient way of writing this (I'm new to RegEx) 我写的正则表达式在下面,可能有一种更有效的书写方式(我是RegEx的新手)
[B][R][0-9][0-9][0-9][0-9]
You can use this: 您可以使用此:
Regex regex = new Regex(@"^BR\d{4}");
^
defines the start of the string (so there should be no other characters before BR) ^
定义字符串的开头(因此,BR之前不应有其他字符) BR
matches - well - BR BR
比赛-很好-BR \\d
is a digit (0-9) \\d
是一个数字(0-9) {4}
says there must be exactly 4 of the previously mentioned group ( \\d
) {4}
表示,前面提到的组( \\d
)中必须恰好有4个 You did not specify what is allowed to follow the four digits. 您未指定允许跟随四位数的内容。 If this should be the end of the string, add a
$
. 如果这应该是字符串的结尾,请添加
$
。
Usage in C#: 在C#中的用法:
string matching = "BR1234";
string notMatching = "someOther";
Regex regex = new Regex(@"^BR\d{4}");
bool doesMatch = regex.IsMatch(matching); // true
doesMatch = regex.IsMatch(notMatching); // false;
BR\d{4}
某些文字至少要回答30个字符:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.