It is necessary to compare the $com variable, which takes only positive integer values ​​(in essence, this is a counter). Must end with 1, but not 11. That is, if $com=1 returns true , if $com=11 returns false , but when $com=21 returns true, I think I am correct, but I’m missing something.

  preg_match('/[^1]*1$/',$com); 
  • And on 511 what should be returned, false? - jekaby
  • Yes, but at 501 true. This is for the algorithm to write the word "comment" after the number. From below, the correct answer was suggested - Zhenya Vedenin

2 answers 2

You can use the negative retrospective test, explicitly demanding that before the last digit 1 there is no other digit 1

 <?php $com = '111'; echo preg_match('/(?<!1)1$/', $com); 
  • 2
    write that it is good because $ com = 1 will pass, but in the original version - no - splash58
  • It helped, only incomprehensible code, absolutely. Not hard to explain? - Zhenya Vedenin
  • one
    Look here this thing (? <! ...) x requires that before x there is no expression ... is called a negative retrospective test. Only 4 types of such checks - positive / negative, anticipatory / retrospective checks. More information can be read either here habrahabr.ru/post/159483 , or in the book J. Friedl Regular expressions. - cheops

The * quantifier allows zero repetition of a character class and is a synonym for {0,} . So it coincides with the zero character, followed by the terminating unit. You obviously need the quantifier + (although under the conditions of the problem it is not needed at all).

  • Well, I just need 0 and more than one units before the unit))). I thought I wrote it - Zhenya Vedenin
  • @ ZhenyaVedenin you understand that 11 in this case can be interpreted as a sequence of "one unit, zero non-units, one unit"? - etki
  • )))) Apparently not) Thank you - Zhenya Vedenin