[英]How can I check for 2 characters with a regular expression?
I used the following to check for two digits. 我使用以下命令检查两位数。
"^\d{2}$"
But how can I change this so I check for two digits or uppercase AZ? 但是,如何更改此设置,以便检查两位数或大写AZ?
2 consecutive digits or 1 uppercase character: 2个连续数字或1个大写字符:
\d{2}|[A-Z]
2 consecutive digits or 2 consecutive uppercase characters: 2个连续数字或2个连续大写字符:
\d{2}|[A-Z]{2}
2 consecutive digit / uppercase characters: 2个连续数字/大写字符:
[\dA-Z]{2}
^[A-Z|\d]{2}$
matches AA and 11 but not A, 1, AAA, or 111 (the AZ specifies uppercase only) 匹配AA和11,但不匹配A,1,AAA或111(AZ仅指定大写字母)
Edit: This will also match 1A and C3 (see comment by Jason), if this is not what you want do not use this answer. 编辑:这也将匹配1A和C3(请参阅Jason的评论),如果这不是您想要的内容,请不要使用此答案。
Use this, will match the items in bold only: 使用此选项,将仅匹配粗体项目:
^\d{2}|[A-Z]{2}$
If you also want to match against negatives, you can try this one: 如果您还想与否定词匹配,可以尝试以下一种方法:
^-?\d{2}|[^-][A-Z]{2}$
And will match these 并会匹配这些
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.