正则表达式匹配特定字符串

时间:2022-05-31 21:39:53

RegEx has always been my Achilles' heel. I am writing web app, where user will input his identifier. I'm using RegexValidator to validate this input.

RegEx一直是我的阿喀琉斯之踵。我正在编写web应用程序,用户将输入其标识符。我正在使用RegexValidator验证此输入。

The identifier should be something like this:

标识符应该是这样的:

TN-In-PL-KW-2012-1234

And this is how the identifier is built:

这就是标识符的构建方式:

  • first two letters are always TN
  • 前两个字母总是TN
  • followed by hyphen
  • 其次是连字符
  • then two letters, that are either: In, Te, Yo or Et
  • 然后是两个字母,分别是:In,Te,Yo或Et
  • hyphen
  • 连字符号
  • two uppercase letters
  • 两个大写字母
  • another hyphen
  • 另一个连字符
  • another two uppercase letters
  • 另外两个大写字母
  • hyphen
  • 连字符号
  • four digits, that are a year, so something between 1970 and 2012 (I can ignore that as long as there are 4 digits)
  • 四位数,即一年,所以在1970年到2012年之间(只要有4位数,我可以忽略它)
  • hyphen
  • 连字符号
  • ordinal number that can have from 1 to 4 digits
  • 序号,可以是1到4位数

Please help me writing RegEx to match this identifier.

请帮我写RegEx以匹配此标识符。

3 个解决方案

#1


7  

^TN-(In|Te|Yo|Et)-[A-Z]{2}-[A-Z]{2}-\d{4}-\d{1,4}$

Just as a comment, I recommend you Rubular if you want to improve your regex skills, it's a simple and practical page to have in mind when you need to work with regex

就像评论一样,如果你想提高你的正则表达能力,我推荐你Rubular,当你需要使用正则表达式时,这是一个简单实用的页面

#2


2  

TN-(In|Te|Yo|Et)-([A-Z]{2}-){2}(19[7-9][0-9]|200[0-9]|201[0-2])-[0-9]{1,4}

TN-(在|特|哟| ET) - ([AZ] {2} - ){2}(19 [7-9] [0-9] | 200 [0-9] | 201 [0-2] ) - [0-9] {1,4}

#3


1  

TN-((In)|(Te)|(Yo)|(Et))-[A-Z]{2}-[A-Z]{2}-\d{4}-\d{1,4}

#1


7  

^TN-(In|Te|Yo|Et)-[A-Z]{2}-[A-Z]{2}-\d{4}-\d{1,4}$

Just as a comment, I recommend you Rubular if you want to improve your regex skills, it's a simple and practical page to have in mind when you need to work with regex

就像评论一样,如果你想提高你的正则表达能力,我推荐你Rubular,当你需要使用正则表达式时,这是一个简单实用的页面

#2


2  

TN-(In|Te|Yo|Et)-([A-Z]{2}-){2}(19[7-9][0-9]|200[0-9]|201[0-2])-[0-9]{1,4}

TN-(在|特|哟| ET) - ([AZ] {2} - ){2}(19 [7-9] [0-9] | 200 [0-9] | 201 [0-2] ) - [0-9] {1,4}

#3


1  

TN-((In)|(Te)|(Yo)|(Et))-[A-Z]{2}-[A-Z]{2}-\d{4}-\d{1,4}