ahsant ahsant - 2 months ago 9
C# Question

Regular expression - dont match consecutive chars - C#

I have wasted enough time to construct a regular expressions which matches.


  1. starts with [a-z]+

  2. ends with [a-z]+

  3. in between can have ' or - or both, as many times as user likes ['-]*

  4. ' should not be consecutive, similarly - should not be consecutive.



thanks for any help in advance.

vks vks
Answer
^(?!.*(?:''|--))[a-z][a-z'-]+[a-z]$

You can add lookahead for the same.See demo.

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