kiran Gopal
kiran Gopal

Reputation: 450

Regex pattern to match number and only one string

I am new to regex and I want to validate create a validator pattern for number and only one string (M|m|T|t|B|b).

Here is the one I have tried /^\d*|(M|m|T|t|B|b){1}

But I am not able to create it. These are the test criteria I want to pass

1234b > pass
1234B > pass
.124b > pass
0.123 > pass
1234bb > fail
12345e > fail

Can anyone help?

Upvotes: 0

Views: 468

Answers (1)

Gurmanjot Singh
Gurmanjot Singh

Reputation: 10360

Try this regex:

(?:\d*\.\d+|\d+)[MTBmtb]{0,1}\b

Click for Demo

Explanation:

  • \d*\.\d+ - matches 0+ digits followed by a decimal followed by 1+ occurrences of a digit
  • | - OR
  • \d+ - matches 1+ digits
  • [MTBmtb]{0,1} - matches 0 or 1 occurrence of either of the the letters - M, T, B, m, t, b
  • \b - a word boundary

Upvotes: 1

Related Questions