[Tutor] Regex's and "best practice"
Lloyd Kvam
pythontutor at venix.com
Sat Nov 22 11:15:25 EST 2003
Carl D Cravens wrote:
> Hello. I'm a Unix sysadmin and developer with a background in C (a few
.....
> ## three formats to deal with (a bare address falls through)
> ## <address> , Name <address>, (Name) address
> $line ~= s/(^<)(.*)(>$)/$2/ ||
> $line =~ s/<.*>// ||
> $line ~=s/(.*)(\()(.*)(\))(.*)/$3/;
>
>
> idpatt = [ re.compile( '(^<)(.*)(>$)' ),
> re.compile( '<.*>' ),
> re.compile( '(.*)(\()(.*)(\))(.*)' ) ]
>
> idrepl = ['\\2', '', '\\3']
>
> oldline = line
> for idpt,idrp in zip( idpatt, idrepl ):
> line = idpt.sub( idrp, line )
> if oldline != line:
> break
>
> Not nearly as compact and simple as the Perl statement.
> Is this pretty much the best I can do? The OR's were very convenient in
> Perl... in Python, I have to do relatively large amount of work to get the
> same effect. (And I don't think it's necessary... I could let Python
> evaluate all threee sub()'s, even when further evaluation won't find
> anything to match.) That would reduce the last block to...
>
> for idpt,idrp in zip( idpatt, idrepl ):
> line = idpt.sub( idrp, line )
>
> ...which doesn't look so bad, but it keeps processing the sub()'s even
> after a match has been made.
>
> Is there something obvious I'm missing, or is this a fair solution to the
> problem?
>
> Thanks!
>
> --
> Carl D Cravens (raven at phoenyx.net)
> Talk is cheap because supply inevitably exceeds demand.
I would call it a fair solution. Perl is very hard to beat on the regex
front simply because regular expressions are in the language, not a module.
For a sysadmin, I think the win with python is the ease of writing reusable
modules that are tailored for your system. I presumme you understand Python's
if __name__ == '__main__':
idiom. This allows a python script to detect that it is being run as a
"command" rather than being imported. So any python script can do double
duty, it's a useful command file AND it is importable to provide useful
subroutines/objects for other scripts.
This is much better than copying and pasting code from one file to another.
--
Lloyd Kvam
Venix Corp.
1 Court Street, Suite 378
Lebanon, NH 03766-1358
voice: 603-653-8139
fax: 801-459-9582
More information about the Tutor
mailing list