开发者

Regex check if number to n-root is not floating point

I am trying to work out how to do something using regular expressions.

Basically, I want to check if a number is equal to a base number (i.e. 2) to the power of n.

For example, I need something thats checks if number i == 2, 4, 8, 16 or 32 then do something.

Edit: The problem lies where the number is actually coming from a varchar column in a legacy database. I could parse it out then do something like 开发者_运维问答kobi reccommended but there is another problem where the number is in a delimited list i.e. (1,2,3,32). Therefore, I thought it would be easier to use regex as it would save a number of steps.

Thanks in advance.


In Python:

import re
a = str(bin(number))
if re.match(r"[^1]*1[^1]*$", a):
    print "power of two"
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜