Akshay Rathod
Akshay Rathod

Reputation: 1683

Regex for only numbers and numbers including slash

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.

Upvotes: 1

Views: 526

Answers (1)

anubhava
anubhava

Reputation: 785541

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

Upvotes: 1

Related Questions