How do I write a regular expression for binary strings such that it's length is a multiple of 3 and this must include the empty string. So for example 010 is true 0101 is false.
3 Answers
Wrap your regex inside the ^ $ so that it performs matching over the whole string.
^([01]{3})*$
1 Comment
zx81
Nice and compact, I have been noticing and like your regex style. Upvoting. :)
Here is what I would suggest:
^(?:[01]{3})*$
It matches the pattern you need, but does not capture the group which is taken into the brackets.
Explanation:
^ // matches beginning of the string
(?: // opens a non-capturing group
[01] // a symbol class, which could only contain 0's or 1's
{3} // repeat exactly three times
) // closes the previously opened group
* // repeat [0, infinity] times
$ // matches the end of the string