Home > other >  Regex for only numbers and numbers including slash
Regex for only numbers and numbers including slash

Time:11-25

I want regex for following possibilities:

01212
111/11
12121221/23445

[0-9]
[0-9]/[1-9]

I am trying ([0-9]|[\/]|[1-9]) and it is working except for zero after slash. I don't want 1212/0

PS: I am new to regex.

CodePudding user response:

You may use this regex:

^0*[1-9]\d*(?:/0*[1-9]\d*)?$

RegEx Demo

RegEx Details:

  • ^: Start
  • 0*: Match 0 or more zeroes
  • [1-9]: Match a non-zero digit
  • \d*: Match 0 more of any digit
  • (?:: Start a non-capture group
    • /: Match a /
    • 0*[1-9]\d*: Match a number not containing all zeroes
  • )?: End non-capture group. ? makes it an optional match
  • $: End
  • Related