People who can make regexps, you flew to us from space! :)
It seems to me that you need to have a specific mindset in order to formalize and implement regexp validators.

So, my task is to create a validator of the following sequences:

  • HJ-1648
  • KM-6617
  • UY-7789

Conditions of the problem:

  1. The prefix of the article should begin only with the given pairs of capital letters. That is, nothing but HJ, KM and UY can be.
  2. Behind the letter sequence there should be a minus as a separator.
  3. Next comes a four-digit numeric sequence that cannot start from scratch.
  4. The length of the resulting string is strictly seven characters.

The preliminary search for a solution was not crowned. Not from the fact that lazy or careless, but from the fact that it is necessary to fly to another galaxy to understand IT :) I spent an hour and a half learning mans in English-speaking and Russian-speaking resources, but only mastered ^[AZ]$ .

  • 2
    And why regexp, if you can check the length, three valid prefixes and then 4 digits? And do not need another mindset, and the code is much clearer. - Vladimir Martyanov
  • one
    @ Vladimir Martianov 1. regex is easier. 2. regex is equally suitable for different places. For example, in the case of a site, the same regex can be used on the server, in the js code on the client, and in the pattern attribute, which modern browsers will check for themselves on their own. - Qwertiy
  • @ Vladimir Martianov, eighty-seven prefixes :) They need to be driven into an array and checked in a loop. In addition, you must write a validator that checks that the length of numeric values ​​is exactly 4 characters. The machine will not understand the numeric value 0600, and for this you will need to use a string. And what can be simpler than a single (!) if not TRegEx.IsMatch(s, '^(HJ|KM|UY)-[1-9][0-9]{3}$') then code if not TRegEx.IsMatch(s, '^(HJ|KM|UY)-[1-9][0-9]{3}$') then ? - Interface Unknown
  • @InterfaceUnknown You already decide - exactly three or 87 :-) And yes, such simple regexp, which is not clear how to make - how? - Vladimir Martyanov
  • one
    @ Vladimir Martianov, 3 is a simplification for the question. For 87 it will be the same after all. - Qwertiy

1 answer 1

I think so ... I even check laziness ...

 ^(HJ|KM|UY)-[1-9][0-9]{3}$ 
  • one
    Optionally, you can replace [0-9] with \d and make the brackets not catchable (this is if you really need to), and you don’t need to check everything :) - tutankhamun
  • @tutankhamun, brackets - yes, it is possible, but I do not see the point. \d in some languages, regional numbers will allow, which seems to be not good on the issue. Also, \d aesthetically not look very close to [1-9] . - Qwertiy
  • @Qwertiy thank you very much. Three minutes were especially impressed by the difference between the question and the answer;) - Interface Unknown
  • With such a detailed TZ and a small amount of work it is not surprising. That would be such projects on freelancing ... Yes, more expensive ... :) - tutankhamun
  • @tutankhamun is, of course, not a project, but only that area of ​​knowledge that Qwertiy (in the specific formulation of the question) owns the best. We are all developers with experience, but sometimes there are tasks that should not be taken head on for different, well-founded reasons. Sometimes you just need to ask your colleagues who can solve these problems many times during the day;) - Interface Unknown