简体   繁体   English

如何使用正则表达式检查2个字符?

[英]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}$
  • 25 25
  • ab AB
  • AB AB
  • 2A 2A
  • A3 A3
  • -23 -23
  • -AB -AB

If you also want to match against negatives, you can try this one: 如果您还想与否定词匹配,可以尝试以下一种方法:

^-?\d{2}|[^-][A-Z]{2}$

And will match these 并会匹配这些

  • 25 25
  • ab AB
  • AB AB
  • 2A 2A
  • A3 A3
  • -23 -23
  • -AB -AB

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何为正则表达式指定可选字符? - How can I specify optional characters for regular expression? 如何使用正则表达式禁止重复字符 - How Can I forbid repeated characters using regular Expression 如何创建需要4个字符且没有空格的正则表达式? - How can I create a regular expression that requires 4 characters and no spaces? 如何检查字符是否属于正则表达式? - How can I check if a character belongs to regular expression or not? 正则表达式检查某些字符 - Regular expression check for some characters 我如何使用.Net正则表达式检查像(9 + 5)*(8/2)这样的表达式? 换句话说,唯一有效的字符是[0-9]和()+-* / - How do i check an expression like this (9+5)*(8/2) using .Net Regular Expression? In other words the only valid characters are [0-9] and ()+-*/ 如何编写一个正则表达式来匹配依赖于已匹配内容的多个字符? - How can I write a regular expression that matches a number of characters dependent on what has already been matched? 使用正则表达式检查空格以外的特殊字符 - Regular expression to check for special characters except space 正则表达式检查带有特殊字符的字符串 - Regular Expression to check a string with special characters 如何检查C#正则表达式是否尝试匹配1个(仅1个)字符的字符串? - How Can I Check If a C# Regular Expression Is Trying to Match 1-(and-only-1)-Character Strings?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM