Prasanth
Prasanth

Reputation: 183

Limit number of characters regex

Trying to figure out a regex which can perform the following

What I got with me is the following:

Edited:

^((([a-zA-Z0-9])*((\/)|(-))?)*[a-zA-Z0-9]$){1,10}

I think it able to perform all operations except the last open i.e. Total length should be between 3 to 10 characters.

Edited:

Examples:

Match: ENG/14-15 , ENG/14/15 , ENG/2014 No Match: ENG//14-15 (adjacent symbols) , ENG/-14-15 (adjacent symbols) , /ENG/14-15/ (Should not start or end with / or -), ENG/2014-15 (11 Characters)

Can anyone help me out with answer and/or explanation?

Regards

Upvotes: 2

Views: 382

Answers (1)

anubhava
anubhava

Reputation: 786261

You can use this lookahead based regex:

^[a-zA-Z0-9](?!.*?[\/-]{2})[a-zA-Z0-9\/-]{1,8}[a-zA-Z0-9]$

Here (?!.*?[\/-]{2}) is negative lookahead that prevents more than one / and - together.

RegEx Demo

Upvotes: 3

Related Questions