user1075940
user1075940

Reputation: 1125

disallow repetition of given set of characters

I need to make a regex which will reject string with any given character in set next to each other

". / - ( )"

For example:

123()123 - false
123--123 - false
124((123 - false
123(123)123-12-12 - true

This is what i have done so far:

(?:([\/().-])(?!.*\1))

Upvotes: 0

Views: 66

Answers (3)

halex
halex

Reputation: 16403

^((?![\/().-]{2}).)*$

This simply negates the regex [\/().-]{2} which matches if two of your characters are next to each other.

See this answer for further explanation.

Live demo

Upvotes: 1

Sujith PS
Sujith PS

Reputation: 4864

You can use :

(^(?:(?![.\/()-]{2}).)*$)

DEMO

Explanation :

enter image description here

Upvotes: 1

Mattias Wadman
Mattias Wadman

Reputation: 11425

Maybe it is easier to do it other way around, match strings you don't want to allow.

if match [.\/()-]{2}
   not allowed
else
   allowed
end

Upvotes: 0

Related Questions