I am a newbie to regex and would like to create a regular expression to check usernames. These are the conditions:
- username must have between 4 and 20 characters
- username must not contain anything but letters a-z, digits 0-9 and special characters -._
- the special characters -._ must not be used successively in order to avoid confusion
- the username must not contain whitespaces
Examples
- any.user.13 => valid
- any..user13 => invalid (two dots successively)
- anyuser => valid
- any => invalid (too short)
- anyuserthathasasupersuperlonglongname => invalid (too many characters)
- any username => invalid because of the whitespace
I've tried to create my own regex and only got to the point where I specify the allowed characters:
[a-z0-9.-_]{4,20}
Unfortunately, it still matches a string if there's a whitespace in between and it's possible to have two special chars .-_ successively:
If anybody would be able to provide me with help on this issue, I would be extremely grateful. Please keep in mind that I'm a newbie on regex and still learning it. Therefore, an explanation of your regex would be great.
Thanks in advance :)

__then as long as your front end is clear about what is a username and what isn't, there is nothing wrong with that username, and it is important to remember that. Wanting to restrict it is fine, but a lot of "common practice" is based on technical limitations from decades ago, effectively none of which still apply if you're writing something new.