简体   繁体   中英

Regular Expression to match specific string

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.

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
  • followed by hyphen
  • then two letters, that are either: In , Te , Yo or 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)
  • hyphen
  • ordinal number that can have from 1 to 4 digits

Please help me writing RegEx to match this identifier.

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

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

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-((In)|(Te)|(Yo)|(Et))-[A-Z]{2}-[A-Z]{2}-\d{4}-\d{1,4}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM