java – Regex for username to include only one special charecter?

So trying to create a regex to accept a username but its restrictions are that the name cannot include two consecutive whitespaces or two consecutives dashes (-).

For example

  • valid: - usernAm3
  • invalid: username (3 spaces)
  • invalid: --username (2 dashes)

So it should get all letters and numbers and only those two special characters but never consecutive

So far I have this

[a-z](?!.* {2})[ w.-]+

https://regex101.com/r/oD3jD3/2

But as you can see from the tests it is allowing for two consecutive dashes (–). Having trouble wrapping head around look around structure.

any suggestions?

Product of the Month September 2016

Source link

Leave a Comment

Your email address will not be published. Required fields are marked *