user3423014
user3423014

Reputation:

regexp to match characters not flowed by a character that is repeated an amount of times

i built this regexp to match letters and numbers and dots
dots only if they are not repeated successfully
example :

something.somethnElse.another.then.something

this is a match because the dots are separated. but in the following case :

something..thensomething

is no match because there is one or more dots next to eachother
this is my regexp, recommend me please why it's not working

[a-zA-Z0-9\.]+(?!\.{2,})

i tried also

[a-zA-Z0-9\.]+(?![\.]+)

but they both give a match for successful dots

Upvotes: 4

Views: 87

Answers (3)

anubhava
anubhava

Reputation: 785196

You are close. You can use this regex:

^(?!.*?\.{2})[a-zA-Z0-9.]+$

RegEx Demo

PS: No need to escape the dot inside character class

Upvotes: 1

hwnd
hwnd

Reputation: 70732

You need to use beginning of string ^ and end of string $ anchors and place the lookahead at the beginning.

/^(?!.*\.{2})[a-z0-9.]+$/i

Live Demo

Upvotes: 1

Ja͢ck
Ja͢ck

Reputation: 173572

First of all, the expression should be anchored, otherwise it only requires a very minimal match.

Additionally, you could think of your expression as a chain of letters and digits that can be interrupted by exactly one dot.

So:

/^(?:[a-z0-9]+|\.(?!\.))*$/

Upvotes: 1

Related Questions