开发者

Regex to match 010101

Is it possible to have a regex to match a string with alternating 0 and 1? It can end in 0 or 1 and le开发者_JS百科ngth 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.


I assume you want

((01)*(0)?)

But the question is very ambiguous


Please provide more info. From the information we have the following would work:

[01]+


(010101)|(010100)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜