Regex no two consecutive characters are the same

后端 未结 2 526
一个人的身影
一个人的身影 2020-12-21 23:04

How do I write a regular expression where x is a string whose characters are either a, b, c but no two consecutive characters are the same

For example

abcacb

2条回答
  •  臣服心动
    2020-12-21 23:49

    Since the number of chars is limited, you can get away without a back reference in the look ahead:

    ^(?!.*(aa|bb|cc)[abc]*$
    

    But I like tenub's answer better :)

提交回复
热议问题