Hello.

I write such a Pattern: (0|15|30|40):(0|15|30|40) , but the UNIT test accepts any numbers (for example: 20: 0 , 100: 0 ). Account must be accepted only with the indicated numbers ( 0, 15, 30, 40) .

I would be grateful for the help because in regular expressions it is not strong at all.

  • one
    put the beginning (^) and end ($) of the line. and just look at your regulars at regexp10.com. it immediately shows that at 20: 0 it works at 0: 0 - Mike
  • Your expression works fine - s.matches("(0|15|30|40):(0|15|30|40)")); . - Wiktor Stribiżew

1 answer 1

You need to set the beginning ^ and end of the line $

 ^(0|15|30|40):(0|15|30|40)$ 

Working example: https://regex101.com/r/PhxrOq/1

  • It needs to be correct: 0-0 (3-5) 30:15 or 0-0 (6-6) 0: 0. And wrong: 0-0 (6-6) 20:30. I am writing this pattern: ^ 0-0 \ ((6-6) | ([0-5] - [0-5]) \) (0 | 15 | 30 | 40) :( 0 | 15 | 30 | 40) $ but he takes a mistake. What is wrong with me? - Nikolay Belyakov
  • @Nikolai Belyakov, and what do you want to withdraw from this line in general? - Yuri
  • I do not want to withdraw anything, but I want to implement a check for a valid account and that this is exactly the account of the first set. - Nikolay Belyakov
  • What do not you like? ideone.com/WAsROI - Kirill Malyshev