IRONLORD
IRONLORD

Reputation: 163

Use regex to match a number between 1 and 105

I've tried several options without success.

(105)|(0*\d{1,2})

This is where I go?

Upvotes: 2

Views: 394

Answers (5)

Vorsprung
Vorsprung

Reputation: 34377

I like the answer above /10[0-5]|[0-9]{1,2}/ but it is not anchored so it will match things like 990 and 1051. It will match as a regexp I mean.

To anchor the regexp use ^ at the start and $ at the end. Here's an improved version of the answer

/^(10[0-5]|\d{1,2})$/

I've used \d which is a commonly available shortcut for [0-9] digits

Edit: see tripleee's comment below, also needs ( ) to group the two alternate expressions

Upvotes: 3

defau1t
defau1t

Reputation: 10619

This could be the better one:

\b0*(10[0-5]|[0-9]{1,2})\b

Takes care of leading Zeros.

Upvotes: 0

Jerry
Jerry

Reputation: 4408

You can always use a match evaluator and check for any condition you want.

Upvotes: 0

Denis de Bernardy
Denis de Bernardy

Reputation: 78523

Perhaps this:

/\b0*([1-9]|[1-9][0-9]|10[0-5])\b/

Upvotes: 3

djf
djf

Reputation: 6757

How about this:

/10[0-5]|[0-9]{1,2}/

  • 10[0-5]: matches '100' to '105' inclusive
  • [0-9]{1,2}: matches ranges '00' to '99' and '0' to '9'

Upvotes: 3

Related Questions