Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex non-consecutive chars

Tags:

regex

Currently I have:

[A-Za-z0-9._%+-]

This matches any string that contains letters, numbers, and certain special chars (._%+-)

How can I change this so that it won't match a string that contains the special chars consecutively?

For example, I want it to match: foo.bar+test and foo.+bar and +foo.

But not: foo..bar+test or foo.bar++test or foo.bar++

like image 253
bjacobs Avatar asked Dec 04 '22 23:12

bjacobs


1 Answers

If your tool/language supports look aheads, try:

^(?!.*([._%+-])\1)[A-Za-z0-9._%+-]+$
like image 168
Bart Kiers Avatar answered Jan 05 '23 09:01

Bart Kiers