Home > other >  Regex doesn't allow two consecutive dashes together (golang)
Regex doesn't allow two consecutive dashes together (golang)

Time:12-22

Can you help me please, my regex shouldn't allow two or more consecutive dashes together(--). All other characters are allowed. Unfortunately negative lookahead doesnt work in golang

I have regex here ^[a-z] (?:[-'\s][a-zA-Z] )*$, but there is several conditions. I need only non-repeating dash

CodePudding user response:

You can use

^[^-] (?:-[^-] )*$

See the regex demo. To allow leading/trailing hyphens:

^-?[^-] (?:-[^-] )*-?$

Details:

  • ^ - start of string
  • [^-] - one or more chars other than -
  • (?:-[^-] )* - zero or more sequences of a - and then one or more chars other than -
  • $ - end of string.

CodePudding user response:

I think you are looking for something like this: .*[-]{2,}.*

  • Related