2

I'm using for exmaple this simple regex:

([A-Za-z0-9!@#]*)

I want to limit the chars !, @ and #, while each of them can appear up to 5 times in the whole string.

Valid strings will be: t!!!sd###ui##pp!@, !@#II#@!00!! etc.

Not valid strings will be: 3!222@34@#@@#@@@!, !@!@!@!@!@!A

Is it possible?

Opu
  • 33
  • 1
  • 6

2 Answers2

1

You can use this pattern:

^(?!(?:[^!]*!){6})(?!(?:[^@]*@){6})(?!(?:[^#]*#){6})[A-Za-z0-9!@#]*$
Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125
0

You need to use lookahead for this. Use this regex:

^(?!(.*?!){6})(?!(.*?#){6})(?!(.*?@){6})[A-Za-z0-9!@#]+$

Online Demo: http://regex101.com/r/cR9yH9

anubhava
  • 761,203
  • 64
  • 569
  • 643