Regular Expression to match specific string

16,666

Solution 1

^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

Solution 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}

Solution 3

TN-((In)|(Te)|(Yo)|(Et))-[A-Z]{2}-[A-Z]{2}-\d{4}-\d{1,4}
Share:
16,666
Admin
Author by

Admin

Updated on June 17, 2022

Comments

  • Admin
    Admin over 1 year

    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.