josh-fuggle
josh-fuggle

Reputation: 3157

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.

Upvotes: 0

Views: 109

Answers (1)

Tim Pietzcker
Tim Pietzcker

Reputation: 336178

In Python:

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

Upvotes: 1

Related Questions