Is it possible to have a regex to match a string with alternating 0 and 1? It can end in 0 or 1 and length is arbitrary.
A regex for all possible binary strings would be ^(0|1)*$
. This includes the empty string. Not including the empty string you would use ^(0|1)+$
.
Is that what you're asking?
Edit: If it's the case that you're looking for alternating 0's and 1's, you can do that as well:
^1?(01)*0?$
should match every possible combination. If you want the string to always start with 0 then you can use ^(01)*0?$
(including empty string) or ^(01)+0?$
(excluding empty string).
Yes, this is possible. (?:01)*0?
will allow an arbitrary amount of "01", optionally followed by a 0, assuming PCRE-like regular expressions with non-capturing groups.
Note that this includes the empty string. If you want at least one character (0), or at least one group of "01", that can also be handled with 0(?:10)*1?
and (?:01)+0?
, respecitvely.
Please provide more info. From the information we have the following would work:
[01]+