scanf style parsing
Andrei Kulakov
sill at optonline.net
Thu Sep 27 13:53:48 EDT 2001
On Thu, 27 Sep 2001 11:32:35 +0000 (UTC), Duncan Booth <duncan at NOSPAMrcp.co.uk> wrote:
> tim at vegeta.ath.cx (Tim Hammerquist) wrote in
> news:slrn9r61oo.uim.tim at vegeta.ath.cx:
>
>> But don't think regex's are disposable just because Python's string type
>> is more convenient. Consider the following:
>>
>> # perl
>> if ($filename =~ /\.([ps]?html?|cgi|php[\d]?|pl)$/) { ... }
>> # python
>> re_web_files = re.compile(r'\.([ps]?html?|cgi|php[\d]?|pl)$')
>> m = re_web_files.search(filename)
>> if m:
>> ...
>>
>> This is a very complicated (but relatively efficient way) to match files
>> with all the folowing extensions:
>> .htm .html .shtm .shtml .phtm .phtml
>> .cgi
>> .php .php2 .php3 .php4
>> .pl
>
> Wouldn't you be happier with this?:
>
> extensions = ['.htm', '.html', '.shtm', '.shtml', '.phtm',
> '.phtml', '.cgi', '.php', '.php2', 'php3', '.php4', '.pl']
> ext = os.path.splitext(filename)[1]
> if ext in extensions:
It would be even better to:
if ext.lower() in extensions:
> ...
>
> which has the arguable advantage of matching what your description says
> instead of what your original code does.
>
> regexes are wonderful: in moderation.
I hate them! :/
>
--
Cymbaline: intelligent learning mp3 player - python, linux, console.
get it at: cy.silmarill.org
More information about the Python-list
mailing list