Ahmad
Ahmad

Reputation: 35

How to limit characters in regex?

I am trying to achieve scenario like that

2014-000000-01

[[0-9]{0,3}\-[0-9]{0,5}\-[0-9]{0,1}]

I want restrict user not to insert more or less than 4 characters before dash After dash he can enter 6 character of less than six and after second dash he can not insert less than or greater than two.

Can any one point out how can i achieve it.

Thanks in Advance

Upvotes: 0

Views: 71

Answers (2)

Brian Stephens
Brian Stephens

Reputation: 5261

Depending on the context, you may need to use begin and end anchors. If it's in a validator, sometimes it's implied to match the entire string. If not, it would still match, even if there were more than 4 digits at the beginning, for instance.

Here it is with anchors: ^\d{4}-\d{1,6}-\d{2}$

Upvotes: 1

nowox
nowox

Reputation: 29066

/[0-9]{4}-[0-9]{0,6}-[0-9]{1,}/

http://regex101.com/r/mC2qM4/1

Upvotes: 0

Related Questions