Help with regex

Nobody nobody at nowhere.com
Fri Aug 7 01:42:29 EDT 2009


On Thu, 06 Aug 2009 14:23:47 -0700, Ethan Furman wrote:

>> [0-9]+ allows any number of leading zeros, which is sometimes undesirable.
>> Using:
>> 
>> 	(0|[1-9][0-9]*)
>> 
>> is more robust.
> 
> You make a good point about possibly being undesirable, but I question 
> the assertion that your solution is /more robust/.  If the OP 
> wants/needs to match numbers even with leading zeroes your /more robust/ 
> version fails.

Well, the OP did say:

> The regex should only match the exact above.

I suppose that it depends upon the definition of "exact" ;)

More seriously: failing to produce an error when one is called for is also
a bug.

Personally, unless I knew for certain that the rest of the program would
handle leading zeros correctly (e.g. *not* interpreting the number as
octal), I would try to reject it in the parser. It's usually much easier
to determine the cause of an error raised by the parser than if you allow
bogus data to propagate deep into the program.




More information about the Python-list mailing list