[Tutor] Additional help

Prasad, Ramit ramit.prasad at jpmorgan.com
Wed Feb 13 21:17:02 CET 2013

Ahmet Can KEPENEK wrote:
> Hello,
> I used regular expression module of python. I checked binary and denary numbers. If input is invalid i
> ask again. I edited your code. Code is below follow as .
>         if re.match("^[0-1]*$", binary):
>         if re.match("^[0-9]*$", denary2):

Both of these will fail for empty strings (or '\n'). 

I will quote Steven D'Aprano's often quoted quotation regarding
regular expressions. (Say that 5 times fast :) )

Some people, when confronted with a problem, think "I know,
     I'll use regular expressions." Now they have two problems.
     -- Jamie Zawinski

I think it would be more accurate (and easier) to convert to 
int and see if the conversion works.

int(binary, 2) # binary = base 2
int(denary2, 10) # denary = base 10


This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  

More information about the Tutor mailing list