[Tutor] Amazing power of Regular Expressions...

Jonathon Sisson sisson.j at gmail.com
Sat Nov 4 14:54:13 CET 2006


Asrarahmed Kadri wrote:
 > tries to supply negative values, then match will return None.
 > So no hassle of using all those Ifs  and Elifs........
 > I think REGULAR Expressions can be quite powerful...

Indeed...Regular expressions were discovered etched into the blade of a 
sword that had been planted into a rock somewhere in ancient 
England...(yes, I'm kidding...but discovering RE's can make you feel 
that way...)

Regular Expressions can be cryptic at first, but once you get the hang 
of them they're addicting... As Alan Gauld stated in another thread (I'm 
pretty sure it was Alan): You'll get to the point that you want to use 
(regular expressions) all the time, even if they aren't the right tool 
for the job.  If something like a string function or a list slice can 
handle the job, don't go overkill on it.

I speak from experience...listen to Alan's advice.  I have used RE's 
where they weren't the right tool, and the deeper I got into the code 
the more I was made aware that RE's were a bad choice.  It's tough to 
dump a week or two's worth of work because of one bad decision...most of 
the time the tendency is to force the bad decision, and this leads to 
bad code...

To summarize:

Regular Expressions:  learn them, love them, use them...but put the 
sword down if a pen can handle the job better.

Jonathon



> 
> Hi Folks,
>  
>  
> I dont know much about Regular Expressions. But I just want to share my 
> ideas.
>  
> I was trying to implement error checking code on the date argument:
>  
> I did this:  
>  
> import re
> # the user should enter date in the format: dd/mm/yyyy
> p = re.compile('\d\d/\d\d/\d\d\d\d')
>  
> m = p.match(date)
>  
> the function match will return an object of type SRE_Match only if the 
> user has supplied the string in the form of dd/mm/yyyy. If the user 


>  
> Regards,
> Asrarahmed
> -- 
> To HIM you shall return.
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor


More information about the Tutor mailing list