Regular Expressions Problem

Brian Szmyd szmyd at colostate.edu
Fri Sep 10 05:18:56 CEST 2004


Oriana wrote:

> Hi!
> 
>   I'm trying to 'clean up' this source file using regular expressions
> in Python. My problem is, that when I try to delete extra lines, my
> code fails. Here's an example....
> 
> /**
> *
> * Project:      MyProject
> *
> *
> *
> *
> *
> *
> *
> * Description:
> *
> *    This file contains the some code.
> *
> * Public Functions:
> *
> *     function_1
> *     function_2
> *
> * Private Functions:
> *
> *    None.
> *
> *
> * Notes:
> *
> *    None.
> *
> *
> *
> *************************************************************************/
> 
> 
> .....I would like my code to only have one * space between lines, and
> not all that white space that I see there. I tried to use the regular
> expression: '^\*\n$^\*\n$' but that does not work. I've tried a bunch
> of things and none of them seem to work....please help!!! Thanks in
> advance, Oriana

Are you reading in the file line by line? If so, why not just have a flag
that states you've seen a empty line, and then if the flag is true, do not
output any more empty lines till you see a non-emtpy line?

Pseudo-Code:

fp = openfile($filename)
op = openfine($newfile)

emptyline = 0
while(not fp.eof())
    line = fp.readline()
    
    if (isEmpty(line))
        if(emptyline) continue          
        emptyline = 1
    else
        emptyline = 0

    op.writeline(line)

Of course you'll have to decide how you want isEmpty() to decide if a string
is an empty line, but this should be pretty painless.

-regards
brian szmyd



More information about the Python-list mailing list