[英]Check whether a string is composed of 3 capital letters and 4 numbers
I need to check if a string is composed by 3 capital letters and 4 digits. 我需要检查一个字符串是否由3个大写字母和4个数字组成。
For example: ABC1234 例如:ABC1234
How do I check it using regular expressions? 如何使用正则表达式检查它?
If you're looking for any order, here are two interesting ways of doing it: 如果您要查找任何订单,可以通过以下两种有趣的方式进行:
^(?=(.*\d){4})(?=(.*[A-Z]){3}).{7}$
"Exactly seven characters, containing 4 digits and 3 capitals". “正好是七个字符,包含4位数字和3个大写字母”。
^(?<d>){4}(?<c>){3}((?<-d>\d)|(?<-c>[A-Z])){7}$
"Expect 4 digits and 3 capitals; then ensure we have exactly 7 digits-or-capitals in total, counting each as we go". “请期待4位数字和3个大写字母;然后确保我们总共总共有7位数字或大写字母,并随我们进行计数”。
I imagine something like this would work: https://regex101.com/r/81ZTVQ/1 我想象这样的事情会起作用: https : //regex101.com/r/81ZTVQ/1
^[AZ]{3}[0-9]{4}$
Explanation: 说明:
[AZ]
- Case sensitive sequential character (capital A to capital Z) match predicate [AZ]
-区分大小写的连续字符(大写A到大写Z)匹配谓词 {n}
- exact number match of preceding match predicate {n}
-先前匹配谓词的精确数字匹配 [0-9]
numeric range (in this case, 0 to 9) match predicate [0-9]
数值范围(在这种情况下,为0到9)匹配谓词 Test cases: 测试用例:
ABC1234 - Full match
A1BC234 - No match
GFQ9230 - Full match
ACB0000 - Full match
Edit: 编辑:
I'd like to include my addition that is somewhat similar to Rawling's answer, yet slightly different. 我想补充一下我的补充内容,该内容与Rawling的回答有些相似,但略有不同。
https://regex101.com/r/81ZTVQ/7 https://regex101.com/r/81ZTVQ/7
^(?=(.*[AZ]){3})(?=(.*\\d){4})([AZ\\d]{7})$
Explanation 说明
(?=(.*[AZ]){3})
- Positive lookahead that matches AZ with any preceding character, 3 times. (?=(.*[AZ]){3})
-与AZ匹配任何前一个字符的正向超前3次。 (?=(.*\\d){4})
- Positive lookahead that matches 0-9 with any preceding character, 3 times. (?=(.*\\d){4})
-与0-9匹配任何前一个字符的正向超前3次。 ([AZ\\d]{7})
- the argument provided must match this predicate - 7 of any combination of AZ and 0-9. ([AZ\\d]{7})
-提供的参数必须匹配此谓词-AZ和0-9的任意组合为7。 I just prefer having it in a group, hence the grouping. Working logically backwards, we can have any combination of AZ and 0-9 as long as the length is 7. Then our positive lookaheads assert the length of the requirements (in this case, 4 digits, 3 capital letters). 从逻辑上向后工作,只要长度为7,我们就可以将AZ和0-9进行任意组合。然后我们的正向断言断言了需求的长度(在这种情况下,为4位数字,3个大写字母)。
Test Cases for Edit 编辑用例
ABC1234
A1BC234
GFQ9230
ACB0000
AGF1923
A237323
3E44E4E
12344AB
AAAE123
AK348A3
Another regex: ^[AZ]{3}\\d{4}$
另一个正则表达式:
^[AZ]{3}\\d{4}$
\\d
stands for digits \\d
代表数字 ^
anchor before first char ^
第一个字符之前的锚点 $
anchor after last char $
锚点位于最后一个字符之后 You can check if a string is composed of 3 capital letters and 4 numbers (assuming those capital letters and numbers can appear anywhere in the string) like this: 您可以检查字符串是否由3个大写字母和4个数字组成(假设这些大写字母和数字可以出现在字符串中的任何位置),如下所示:
Regex rgx = new Regex(@"(^[A-Z]{3}\d{4}$)|(^[A-Z]{2}\d[A-Z]\d{3}$)|(^[A-Z]{2}\d{2}[A-Z]\d{2}$)|(^[A-Z]{2}\d{3}[A-Z]\d$)|(^[A-Z]{2}\d{4}[A-Z]$)|(^[A-Z]\d[A-Z]{2}\d{3}$)|(^[A-Z]\d[A-Z]\d[A-Z]\d{2}$)|(^[A-Z]\d[A-Z]\d{2}[A-Z]\d$)|(^[A-Z]\d[A-Z]\d{3}[A-Z]$)|(^[A-Z]\d{2}[A-Z]{2}\d{2}$)|(^[A-Z]\d{2}[A-Z]\d[A-Z]\d$)|(^[A-Z]\d{2}[A-Z]\d{2}[A-Z]$)|(^[A-Z]\d{3}[A-Z]{2}\d$)|(^[A-Z]\d{3}[A-Z]\d[A-Z]$)|(^[A-Z]\d{4}[A-Z]{2}$)|(^\d[A-Z]{3}\d{3}$)|(^\d[A-Z]{2}\d[A-Z]\d{2}$)|(^\d[A-Z]{2}\d{2}[A-Z]\d$)|(^\d[A-Z]{2}\d{3}[A-Z]$)|(^\d[A-Z]\d[A-Z]{2}\d{2}$)|(^\d[A-Z]\d[A-Z]\d[A-Z]\d$)|(^\d[A-Z]\d[A-Z]\d{2}[A-Z]$)|(^\d[A-Z]\d{2}[A-Z]{2}\d$)|(^\d[A-Z]\d{2}[A-Z]\d[A-Z]$)|(^\d[A-Z]\d{3}[A-Z]{2}$)|(^\d{2}[A-Z]{3}\d{2}$)|(^\d{2}[A-Z]{2}\d[A-Z]\d$)|(^\d{2}[A-Z]{2}\d{2}[A-Z]$)|(^\d{2}[A-Z]\d[A-Z]{2}\d$)|(^\d{2}[A-Z]\d[A-Z]\d[A-Z]$)|(^\d{2}[A-Z]\d{2}[A-Z]{2}$)|(^\d{3}[A-Z]{3}\d$)|(^\d{3}[A-Z]{2}\d[A-Z]$)|(^\d{3}[A-Z]\d[A-Z]{2}$)|(^\d{4}[A-Z]{3}$)");
var samples = new string[]{ "ABC1234", "1A2B3C4", "ABCD123", "aBC1234", "ABC12345" };
foreach(var sample in samples)
{
Console.WriteLine(sample + " is " + (rgx.IsMatch(sample) ? "a match" : "not a match"));
}
Output: 输出:
ABC1234 is a match
ABC1234是一场比赛
1A2B3C4 is a match1A2B3C4是一个匹配项
ABCD123 is not a matchABCD123不匹配
aBC1234 is not a matchaBC1234不匹配
ABC12345 is not a matchABC12345不是一个匹配项
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.