Home > Enterprise >  regex for 2 numbers and a sign
regex for 2 numbers and a sign

Time:10-21

I need a preg_match with regex for control a number input.

User has to enter a number between 1-80. But after 1-80 so the 3rd input can be a sign or ^.

So 1-80 is possible, but 79 or 61^ has to be possible

Is this possible with regex?

My code now:

[0-7][0-9]|80

But now i cant enter 1-9 in the input. Who can help me

CodePudding user response:

The part [0-7][0-9] can also match 00 and matches at least 2 characters.

You can use

^(?:[1-9]|[1-7][0-9]|80)[ ^]?$
  • ^ Start of string
  • (?: Non capture group
    • [1-9] Match a digit 1-9
    • | Or
    • [1-7][0-9] Match 10-79
    • | Or
    • 80 Match literally
  • ) Close group
  • [ ^]? Optionally match or ^ (where ^ should not be at the start of the character class)
  • $ End of string

Regex demo

  • Related