Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expression for not allowing two consecutive special characters

Tags:

regex

What i am trying to do is to not allow two consecutive special characters like &* or *$ or &&, but it should allow special characters in between strings like Hello%Mr&.

What i have tried so far:

^(([\%\/\\\&\?\,\'\;\:\!\-])\2?(?!\2))+$
like image 789
Waqar Ahmed Avatar asked Mar 02 '26 02:03

Waqar Ahmed


1 Answers

^(?!.*[\%\/\\\&\?\,\'\;\:\!\-]{2}).*$

The idea is to use a negative lookahead ((?!)) to verify that nowhere in the string (.*) are there two consecutive "special" characters ([...]{2}). Afterwards, you just match the entire string (.*).

like image 81
ndnenkov Avatar answered Mar 03 '26 18:03

ndnenkov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!