1

It is not possible to create a regular expression of this type xx.xx.xxx, where x - can be any Latin or Russian character of any register or digit. But there must be 2 symbols, then the dot => 2 symbols => point => 3 characters Made the following expression -

var_dump(preg_match('/^([а-я]*[А-Я]*[A-Z]*[a-z]*ё*Ё*[0-9]*){2}.([а-я]*[А-Я]*[A-Z]*[a-z]*ё*Ё*[0-9]*){2}.([а-я]*[А-Я]*[A-Z]*[a-z]*ё*Ё*[0-9]*){3}$/u', 'd1.df.dfd'));

The expression works correctly, but if you delete 1 character at the end, for example d1.df.df, it returns 1, although it should 0. Tell me please what is the problem?

tirael8
  • 213
  • 4
  • 14

1 Answers1

0

The ([а-я]*[А-Я]*[A-Z]*[a-z]*ё*Ё*[0-9]*){2} pattern part matches 0 or more letters from а to я, then 0+ chars А to Я, etc. All that can match 0 or more times (see the * quantifier after ) that creates a repeated capturing group, so, the captures always only contain empty strings).

What you need is to "merge" all character classes inside each group into single character class, and apply the limiting quantifier to the class:

'~^([а-яА-ЯA-Za-zёЁ0-9]{2})\.([а-яА-ЯA-Za-zёЁ0-9]{2})\.([а-яА-ЯA-Za-zёЁ0-9]{3})$~u'

See the regex demo

With a case insensitive modifier, it will be a bit shorter:

'~^([а-яa-zё0-9]{2})\.([а-яa-zё0-9]{2})\.([а-яa-zё0-9]{3})$~ui'

Also, you may shorten the pattern using subroutines:

'~^(([а-яa-zё0-9]){2})\.((?2){2})\.((?2){3})$~ui'

See another regex demo. Herem (?2) repeats the pattern inside Capturing group #2, ([а-яa-zё0-9]).

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563