Reputation: 400
I'm looking for a regex, which fits to every number, that is made from 0,1,2 and don't have the same digits beside - 02021 fits, 0122 doesn't fit. How it can be written?
Upvotes: 1
Views: 85
Reputation: 163207
You can start with a digit between zero and two. Capture that in a group and check that the following is not the captured value. Capture that in a non capturing group and repeat that.
Explanation
^
(?:
(
[0-2]
)
(?!
)
)
*
$
Upvotes: 5
Reputation: 12438
You can use the following regex:
^(([012])(?!\2))+$
tested here: https://regex101.com/r/6vevDl/1
Upvotes: 1