Regular Expression - Double Spaces Prevention

I have a big regex for a name field that looks like this.

^(?:(?!(?:.*[ ]){2})(?!(?:.*[']){2})(?!(?:.*[-]){2})(?:[a-zA-Z0-9 \p{L}'-]{3,48}$))$

I'm not a regex expert, I got this so far with Stackoverflow and RegexBuddy. But there is one line that I come across. The first positive result, (?!(?:.*[ ]){2})this prevents the presence of several spaces.

This is not quite what I want. I just want to make sure there cannot be several spaces in the sequence . Like double spaces and the like. This regex prevents more than 1 space in the entire line.

I'm trying to figure out how to change this, but I'm really at a dead end. Is there a way to enforce this concept with the rest of the regular expression?

C # where this will be done.

+3
source share
1 answer

Replace (?!(?:.*[ ]){2})with(?!.*[ ]{2})

Explanation:

(?:.*[ ]){2}first matches one space preceded by zero or more other characters ( (?:.*[ ])), which is then repeated twice ( {2}).

.*[ ]{2} matches two consecutive spaces preceded by zero or more other characters.

+4
source

All Articles